Recent

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

torbente

  • Sr. Member
  • ****
  • Posts: 325
    • Noso Main Page
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
Noso Cryptocurrency Main Developer
https://github.com/DevTeamNoso/NosoWallet

Mike.Cornflake

  • Hero Member
  • *****
  • Posts: 1260
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: 1131
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 2.06 (64b) / FPC 3.0.4 / Windows 10
stucked on Delphi 10.3.1

torbente

  • Sr. Member
  • ****
  • Posts: 325
    • Noso Main Page
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.
Noso Cryptocurrency Main Developer
https://github.com/DevTeamNoso/NosoWallet

wp

  • Hero Member
  • *****
  • Posts: 11923
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');

torbente

  • Sr. Member
  • ****
  • Posts: 325
    • Noso Main Page
Re: Selected row in a Tlistview
« Reply #5 on: September 08, 2014, 06:36:55 pm »
I got it! Learning fast! Thanks!
Noso Cryptocurrency Main Developer
https://github.com/DevTeamNoso/NosoWallet

 

TinyPortal © 2005-2018