Question

I have a TListView descendant which introduces additional features like sorting and loading from a dataset.

I now wish to modify the class so that whenever an item is checked or unchecked it is added or removed in an internal list of checked items so that an application using an instance of the component can easily read the number and location of checked items without having to iterate over all the items in the list view.

Unfortunately, rather than abstracting handling of the check/uncheck operation into some internal method (like DoCheck) that I could override, TCustomListView seems to embed the check logic deep in a large message-handling function.

The only way I can think of to introduce my own behavior neatly into the component is to hijack the OnItemChecked property, causing that property to read and write FOnItemCheckedUser (for example) rather than FOnItemChecked, put my own code in FOnItemChecked, and call FOnItemCheckedUser from my code.

Is there a more straightforward way to handle this? If not, is my idea feasible and safe to implement?

Was it helpful?

Solution

It's unfortunate that the check-event code is buried in a message handler, but it's not a showstopper. Catch and handle that same message yourself. Detect the same conditions the parent class's message handler checks for, and then do your custom actions there. Afterward, call inherited.

type
  TListViewDescendant = class(TListView)
  private
    procedure CNNotify(var Message: TMessage); message cn_Notify;
  end;

procedure TListViewDescendant.CNNotify(var Msg: TMessage);
begin
  if IsCheckBoxNotification(Msg) then
    DoSpecialCheckBoxHandling;
  inherited;
end;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top