Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delphi TListview - search for item and auto-scroll to that item

Delphi 10.4.1 with FireMonkey.

I have 50 items on a TListView, with IDs from 1 to 50. No code posted here because I don't even know where to get started.

If I did a search for ID=35, is there a way to make TListView move exactly to the position of that item programmatically so that the item is in full view?

like image 489
Peter Jones Avatar asked Sep 20 '25 10:09

Peter Jones


2 Answers

This is trivial:

ListView.ScrollTo(Item.Index);
like image 97
Schneider Infosystems Ltd Avatar answered Sep 21 '25 23:09

Schneider Infosystems Ltd


In short: Set the SelectedItem and the list view scrolls to this element in the list.

In long: You can iterate through the list and select the item that matches your search criteria:

for var c := 0 to ListView1.ItemCount - 1 do
  if ListView1.Items[c].Text = <TextToSearch> then
  begin
    ListView1.Selected := ListView1.Items[c]; 
    break;
  end;

Unfortunately, you do not explain how to save the ID in the list item. My example assumes that this is done in the list text. Alternatively you can also use e.g. Tag or TagString.