Re: Delphi listbox - substrings

Posted by Creeek on December 14, 2005

In Reply to: Re: Using Delphi listbox posted by delphiman on November 22, 2005

I have recently created this function to add to substrings, which effectively add to columns!!
NOTE: Extra columns are added under the 'columns' property in the ListView object inspector.

Not pretty code, but easy to understand! It can be changed for less columns. Obviously it could be more elegant, using an array or Tstrings to call it with, but this is easier to understand

procedure TMain.AddToListItem(ListViewItem: TListView; 
  Str1, Str2, Str3, Str4, Str5, Str6, Str7, Str8, Str9: string);
  ListItem : TListItem;
  with ListViewItem do begin
    ListItem := ListViewItem.Items.Add;
    ListItem.Caption := Str1; // 1st column to the left
    if Str2 <> '' then ListItem.SubItems.Add(Str2);
    if Str3 <> '' then ListItem.SubItems.Add(Str3);
    if Str4 <> '' then ListItem.SubItems.Add(Str4);
    if Str5 <> '' then ListItem.SubItems.Add(Str5);
    if Str6 <> '' then ListItem.SubItems.Add(Str6);
    if Str7 <> '' then ListItem.SubItems.Add(Str7);
    if Str8 <> '' then ListItem.SubItems.Add(Str8);
    if Str9 <> '' then ListItem.SubItems.Add(Str9);

A call example, where Listfiles is the name of the TListview component:

AddToListItem(ListFiles, '1st   col', 'col2','Column3,'','','','', '', ''); 

null strings allow for Listview box's with smaller amount of columns

It works fine for me :)