Search code examples
delphivcldelphi-10.1-berlin

What event to call when ListView selected item changed?


I'd like to run a procedure--once--whenever the selected item in a ListView changes. The obvious choice would be OnSelectItem, but it's called twice when the user moves from one selected item to another (using mouse or arrow keys). Similarly, OnChange is called three times when moving between items.

Is there an event generated only once under these conditions? OnClick is generated once, but doesn't cover moving between items using arrow keys, etc.


Solution

  • You can do it like this using OnSelectItem.

    • Remember the last selected item.
    • When the OnSelectItem fires, check if the current selected item differs from the one you remembered.
    • If so, perform your task, and make a note of the new selected item.