Question

I've got a TListView and several controls on a form representing data. When the selected item in the TListView changes, I want to validate the data and save it back to the backing store before changing to display the new record. But there doesn't appear to be any event handler to hook into. Turns out that the one that looks obvious, OnChanging, which even includes a way to abort the change, isn't about changing your selection at all; it's about editing the current item.

Is there any way I can do validation before changing the current selection on a TListView?

Was it helpful?

Solution

The event you are looking for is OnSelectItem. It fires once for the item being de-selected, and again for the item being selected.

procedure SelectItem(Sender: TObject; Item: TListItem; Selected: Boolean);

Here, Sender is the list view control, Item is the item being selected or de-selected, and Selected indicates whether this is selection or de-selection.

If you want to block selection change then OnChanging is indeed the event you need. Check the Change parameter. It has value ctState when the selection is changing. Set the AllowChange parameter to False to block the change.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top