How to respond when (before) the selected item in a TListView changes?

StackOverflow https://stackoverflow.com/questions/21686152

  •  09-10-2022
  •  | 
  •  

Вопрос

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?

Это было полезно?

Решение

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.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top