Delphi - Get current index of selected item in TListView

14,026

Solution 1

Use the ItemIndex property.

A value of -1 indicates no selection.

From documentation:

Read ItemIndex to determine which item is selected. The first item in the list has index 0, the second item has index 1, and so on. If no item is selected, the value of ItemIndex is -1. If the list control supports multiple selected items, ItemIndex is the index of the selected item that has focus.

Solution 2

Use Index property of Selected item

if lvClients.Selected <> nil then
  index := lvClients.Selected.Index;
Share:
14,026
Moussamoa
Author by

Moussamoa

Updated on June 06, 2022

Comments

  • Moussamoa
    Moussamoa about 2 years

    I have a TListView in a form and I would like to know the index of the selected item. I tried to find a method or a property of my TListView which gives that information but the only thing I found was lvClients.Selected and it doesn't give the index of this item.

    Can someone help me to find how to get the index of the selected item in my TListView ? Thanks.

  • Moussamoa
    Moussamoa about 9 years
    Thanks, I wonder why it doesn't appear in the code autocompletion when I type it...
  • LU RD
    LU RD about 9 years
    You're welcome. It does show up in autocompletion in XE7 at least.
  • SilverWarior
    SilverWarior about 9 years
    It should appear in code autocompletion just fine. It does so in every Delphi version I know and I started back in Delphi 6. The only reason why this couldn't work is if you are hitting bug that was present in some Delphi versions where code insight didn't work properly on really big projects or if you have some syntactical error in the code somewhere above your current code position as this can prevent code insight to properly scan whole unit.
  • Moussamoa
    Moussamoa about 9 years
    I'm on Delphi 6 and the project I am working on is really big (several million lines of code). Maybe that's the reason why it doesn't work properly. Thanks for the extra info.
  • Remy Lebeau
    Remy Lebeau about 9 years
    Which is exactly how the TListView.ItemIndex property getter is implemented internally.
  • zac
    zac about 6 years
    this 2nd line gave me AV !!