Delphi – Listview Subitem Images

delphilistview

Am I missing something obvious here, I cannot find a way to assign an ImageIndex to a Subitem of a TListView.

I have the Listview set in vsReportMode with 2 columns, I can easily assign an ImageIndex to the first column Items, something like:

ListView1.Items[0].ImageIndex := 0;
ListView1.Items[1].ImageIndex := 1;
ListView1.Items[2].ImageIndex := 2;

I thought I could assign an ImageIndex to it's SubItems, something like this (which obviously does not work because the property does not seem to exist with SubItems)

ListView1.Items[0].SubItems[0].ImageIndex := 0;
ListView1.Items[1].SubItems[0].ImageIndex := 1;
ListView1.Items[2].SubItems[0].ImageIndex := 2;

Am I confusing myself again or is there no such property for SubItem Images?

Best Answer

Use SubItemImages instead:

var
  LI: TListItem;
  i: Integer;
begin
  ListView1.ViewStyle := vsReport;
  for i := 0 to 1 do
    with ListView1.Columns.Add do
      Caption := 'Column ' + IntToStr(i);

  for i := 0 to ImageList1.Count - 1 do
  begin
    LI := ListView1.Items.Add;
    LI.Caption := Format('Item %d', [i]);
    LI.ImageIndex := i;
    LI.SubItems.Add(Format('SubItem %d', [i]));
    LI.SubItemImages[0] := i; // SubItems[ColumnIndex] := ImageIndex;
  end;
end;

This results in

ListView with SubItems and Images

Related Topic