Forum > General

[Listview] How to add subitems to filled listview?

(1/2) > >>

krexon:
I have 3 columns and 20 rows in listview. Two first columns are filled with numbers.
How to add text to third column beginning from first row?
I tried like this:

--- Code: ---var
  listitem: TListItem;
  s: string;
begin
// ....
  with lv do
  begin
    listitem:=Items.Item[lp];
    listitem.SubItems[0]:=s;
  end;
--- End code ---
but this adds text to second column. When I change to ...Subitems[1]:=s; then I have error: List index (1) out of bounds

Please help me :)

skalogryz:

--- Code: ---  with lv do
  begin
    listitem:=Items.Item[lp];
    listitem.Caption:=s;
  end;

--- End code ---

krexon:
But I don't wanna change first column (caption), but third :) The first two are already filled.

skalogryz:

--- Quote from: krexon on August 23, 2010, 02:05:49 pm ---But I don't wanna change first column (caption), but third :) The first two are already filled.

--- End quote ---
You need to check listitem.SubItems.Count and add missing sub-items if necessary.

--- Quote ---ith lv do
  begin
    listitem:=Items.Item[lp];
    listitem.Caption:='first';
    listitem.SubItems.Add('second');
    listitem.SubItems.Add('third');
    listitem.SubItems.Add('fourth');
  end;

--- End quote ---

fabienwang:
Why not use something like that?


--- Code: ---procedure TForm1.Button1Click(Sender: TObject);
var i: Integer;
    listitem: TListItem;
    s: String;
begin
  i:= 0;
  for i:= 0 to 20 do
  begin
    listitem:= TListItem.Create(ListView1.Items);
    listitem.Caption := IntToStr(i);
    listitem.SubItems.Add('col2 ' + IntToStr(i));
    listitem.SubItems.Add('col3 ' + IntToStr(i));
    ListView1.Items.AddItem(listitem);
    //listitem.SubItems[0]:= 'test2';
  end;
end;            
--- End code ---

Sorry for double post skalogryz ^^

Navigation

[0] Message Index

[#] Next page

Go to full version