Jump to content
Ian Branch

Justification for Header different to resto column in TListView?

Recommended Posts

Hi Team,

I have a TListview that currently looks like this.

image.png.e07ac23b60eea751501fe4763c13988d.png

I have tried to set the first column to Center justify but it keeps reverting to Left Justify. 😞

What I would like is for the first column to be center justified, and the second colmn's header to be center but the rest of column to be right.

I would also like the Header font to be Bold as well, but there doesn't appear to be any 'Font' related property.  😞

Is this doable?

If so, how please?

 

Regards & TIA,

Ian

Share this post


Link to post

Just create first column without caption and set its width to 0.

The others do what you want to do.

Bild_2023-05-01_225826817.png

Share this post


Link to post
procedure TForm1.Button1Click(Sender: TObject);
var
  MyListColumn: TListColumn;
  MyListItem  : TListItem;
begin
  ListView1.Columns.Clear;
  ListView1.ViewStyle := TViewStyle.vsReport;
  //
  MyListColumn           := ListView1.Columns.Add; // 0
  MyListColumn.Width     := 150;
  MyListColumn.Alignment := TAlignment.taLeftJustify; // alignment of the leftmost column is always LVCFMT_LEFT
  MyListColumn.Caption   := 'LeftJustify';
  MyListColumn.Width     := 0; // a "hack"
  //
  MyListColumn           := ListView1.Columns.Add; // 1
  MyListColumn.Width     := 150;
  MyListColumn.Alignment := TAlignment.taCenter;
  MyListColumn.Caption   := 'CenterJustify';
  //
  MyListColumn           := ListView1.Columns.Add; // 2
  MyListColumn.Width     := 150;
  MyListColumn.Alignment := TAlignment.taRightJustify;
  MyListColumn.Caption   := 'RightJustify';
  //
  MyListColumn           := ListView1.Columns.Add; // 3
  MyListColumn.Width     := 150;
  MyListColumn.Alignment := TAlignment.taCenter;
  MyListColumn.Caption   := 'CenterJustify';
  //
  // 4 columns:  0, 1, 2, 3:
  MyListItem               := ListView1.Items.Add;
  MyListItem.Caption       := 'Item1';     // Column 0
  MyListItem.SubItems.Text := 'SubItem1';  // Column 1
  MyListItem.SubItems.Add('SubItem1.1');   // Column 2
  MyListItem.SubItems.Add('SubItem1.1.1'); // Column 3
end;

 

image.png

Edited by programmerdelphi2k

Share this post


Link to post

https://learn.microsoft.com/en-US/windows/win32/api/commctrl/ns-commctrl-lvcolumna?redirectedfrom=MSDN

 

Alignment of the column header and the subitem text in the column. The alignment of the leftmost column is always LVCFMT_LEFT; it cannot be changed. This member can be a combination of the following values. Note that not all combinations are valid.

Share this post


Link to post
7 hours ago, programmerdelphi2k said:

Your "hack"

All of that is not needed, just do it in the Object Inspector like I told.

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×