Recent

Author Topic: [Listview] How to add subitems to filled listview?  (Read 15205 times)

krexon

  • Jr. Member
  • **
  • Posts: 80
[Listview] How to add subitems to filled listview?
« on: August 23, 2010, 12:48:04 pm »
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: [Select]
var
  listitem: TListItem;
  s: string;
begin
// ....
  with lv do
  begin
    listitem:=Items.Item[lp];
    listitem.SubItems[0]:=s;
  end;
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

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2770
    • havefunsoft.com
Re: [Listview] How to add subitems to filled listview?
« Reply #1 on: August 23, 2010, 01:45:11 pm »
Code: [Select]
  with lv do
  begin
    listitem:=Items.Item[lp];
    listitem.Caption:=s;
  end;

krexon

  • Jr. Member
  • **
  • Posts: 80
Re: [Listview] How to add subitems to filled listview?
« Reply #2 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.

skalogryz

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2770
    • havefunsoft.com
Re: [Listview] How to add subitems to filled listview?
« Reply #3 on: August 23, 2010, 02:13:21 pm »
But I don't wanna change first column (caption), but third :) The first two are already filled.
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;

fabienwang

  • Sr. Member
  • ****
  • Posts: 449
  • Lazarus is the best
    • My blog
Re: [Listview] How to add subitems to filled listview?
« Reply #4 on: August 23, 2010, 02:18:21 pm »
Why not use something like that?

Code: [Select]
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;            

Sorry for double post skalogryz ^^
I'm using Arch Linux.
Known for: CPickSniff, OpenGrabby
Contributed to: LazPaint

krexon

  • Jr. Member
  • **
  • Posts: 80
Re: [Listview] How to add subitems to filled listview?
« Reply #5 on: August 23, 2010, 02:22:51 pm »
Thanks for proposal.
Meantime I found solution :)

Code: [Select]
listitem.SubItems.Insert(1,s)

 

TinyPortal © 2005-2018