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

Viewed 1246

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?

2 Answers

This is trivial:

ListView.ScrollTo(Item.Index);

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.

Related