addHandler doesn't call delegate methods

I have defined a Class Interface for a ‘ListBoxDelegate’, for classes that need to handle certain events on behalf of a listbox.
On a Listbox subclass called MyListBox, I add a getter/setter pair and a private mListBoxDelegate property of type ListBoxDelegate:

// Remove previous delegates first when needed
If mListBoxDelegate <> Nil Then
  RemoveHandler ExpandRow, WeakAddressOf mListBoxDelegate.onListExpandRow
  RemoveHandler CollapseRow, WeakAddressOf mListBoxDelegate.onListCollapseRow
  RemoveHandler DoubleClick, WeakAddressOf mListBoxDelegate.onListDoubleClick
End If

// Set the delegate
mListBoxDelegate = value

If mListBoxDelegate <> Nil Then
  AddHandler ExpandRow, WeakAddressOf mListBoxDelegate.onListExpandRow
  AddHandler CollapseRow, WeakAddressOf mListBoxDelegate.onListCollapseRow
  AddHandler DoubleClick, WeakAddressOf mListBoxDelegate.onListDoubleClick

End If

I have a MyListBoxController-class that implements the Class Interface.
I then set the delegate for the MyListBox-instance in the constructor of MyListBoxController.

myListbox.listboxDelegate= me

However none of the delegate methods get called.
When I overrule the event by adding it using the IDE, I get a runtime error saying the event already gets handled. This is normal.
When I hit a breakpoint during debugging the mListBoxDelegate of the listbox points to the right listboxcontroller.

You need to provide some complete sample (just the issue, isolated) so people can run and observe what you are saying and they can try to figure out what’s going on. Maybe what’s going on is located somewhere else in another layer outside this part, like an object out of scope being destroyed and disabling things.

I’m going to make a suggestion. Instead of using AddHandler for all this stuff,

  1. add a class interface to your project, called ListboxDelegate.
  2. Add the three methods to the interface
  3. Add a property to your subclass (let’s say DelegateObj for this example) with the type set to ListboxDelegate.
  4. In each of the events on your listbox, check if DelegateObject is nil, and if not, call the corresponding method.

Now all you need to do is apply the interface to the things that could receive the events and set the DelegateObj property to that to have it receive the events… and you don’t have to be concerned with having the better/setter pair or calling RemoveHandler to prevent leaks.


I did what you described,
basically replace the addHandler commands with regular event handlers and in them
forward the events to the DelegateObj if it was set.

I started working again :slightly_smiling_face:

But now I’m even more confused :confused:
the fact that simply replacing the addHandler with a regular eventHandler worked, proves to me that the rest of the application was not to blame, also why isn’t addHandler working as a 1-on-1 replacement for the normal eventHandler???

Listbox is “special” in many ways and it’s been pointed out to me that using AddHandler with listbox like this has been broken for a number of years.