Recent

Author Topic: Selected row in a Tlistview  (Read 3576 times)

torbente

  • Full Member
  • ***
  • Posts: 229
Selected row in a Tlistview
« on: September 08, 2014, 12:12:27 pm »
I have a tlistview with this properties:

viewstyle:= vsreport
rowselect := true
Multiselect := false

How i can get the the selected row?

Thanks in advance

Mike.Cornflake

  • Hero Member
  • *****
  • Posts: 1227
Re: Selected row in a Tlistview
« Reply #1 on: September 08, 2014, 12:54:34 pm »
Just tested.  ListView.ItemIndex works for me under Windows.
Lazarus Trunk/FPC Trunk on Windows [7, 10]
  Have you tried searching this forum or the wiki?:   http://wiki.lazarus.freepascal.org/Alternative_Main_Page
  BOOKS! (Free and otherwise): http://wiki.lazarus.freepascal.org/Pascal_and_Lazarus_Books_and_Magazines

mangakissa

  • Hero Member
  • *****
  • Posts: 888
Re: Selected row in a Tlistview
« Reply #2 on: September 08, 2014, 01:13:14 pm »
Isn't TListview.Selected the more proper way?
Code: [Select]
var Item : TListItem;
begin
  Item := Listview1.selected;
  showmessage(Item.text);
end;
Lazarus 1.84 (32b) / FPC 3.0.4
Windows 10

torbente

  • Full Member
  • ***
  • Posts: 229
Re: Selected row in a Tlistview
« Reply #3 on: September 08, 2014, 05:47:56 pm »
Both worked, thanks.

Im very new to listviews, im playing with them just now, i used stringgrids in the past, but listviews looks more indicate for some tasks i need add.

How is the access to a specified string? stringgrids uses grid.cells , are there something like that in listviews?

Again, thanks a lot.

wp

  • Hero Member
  • *****
  • Posts: 5452
Re: Selected row in a Tlistview
« Reply #4 on: September 08, 2014, 06:02:33 pm »
Yes, that's more compliated than with a StringGrid. But the ListView is worth learning because it is very close to the TreeView which can be very, very important:

The rows that you see in a TListView are TListItems. The "caption" of a TListItem appears in the first column. Each ListItem can have SubItems which appear in the subsequent columns.

To create the strings you use
Code: [Select]
var
  item: TListItem;
...
  item := Listview1.Items.Add;  // this creates a ListItem and adds it
  item.Caption := 'Text of 1st column';
  item.SubItems.Add('Text of 2nd column');  // SubItems is a "stringList" --> use "Add"
  item.SubItems.Add('Text of 3rd column');

To modify the string in the n-th column of ListItem with Index I you use (not tested - no guarantee...)
Code: [Select]
var
  item: TListItem;
begin
  item := Listview1.Items[I];
  if n = 0 then
    item.Caption := 'New text'
  else
  if n < ListView1.Items.Count-1 then
    Item.SubItems[n-1] := 'New text'
  else
    raise Exception.Create('This column does not exist');
Lazarus trunk / fpc 3.0.4 / all 32-bit on Win-10

torbente

  • Full Member
  • ***
  • Posts: 229
Re: Selected row in a Tlistview
« Reply #5 on: September 08, 2014, 06:36:55 pm »
I got it! Learning fast! Thanks!