Is it possible to add a stock Event Handler at run-time?

Hi all, is there a way to add an event handler to a ListBox at runtime? I want to add HeaderBackgroundPaint. The reason I need to do it at run-time is it is not supported in Xojo 2015r4.1 which I still need to use to generate a 32-bit version using the old graphics systems for certain customers. When I build with the old version, it gives an error that it is already implemented in the Super class of the ListBox.

I looked at AddHandler, but I don’t think that is correct for a stock event I want to add at run time. Any help would be greatly appreciated, while I dig through the docs some more.

AddHandler may work, but please add it as early as possible, e.g. in open event.
And put a #if XojoVersion > 2016 or so around.

1 Like

Thanks Christian, I’ll take a deeper look at AddHandler.

We’ve done things like that quite often (e.g. to support HiDPI, DarkMode in code that compiles with both old/new Xojo Versions). AddHandler is what’s needed.

I’ve quickly tried… and it seems that in the case of Listbox, that’s already too late.

What works is to subclass Listbox, and add it in the Constructor.

Here is a quick and dirty example project you can look at:

  • MyListbox is a Subclass of Listbox
  • Constructor: AddHandler (for supported Xojo Versions only)
  • used WeakAddressOf, so that Listbox can be closed… as I’ve said: quick and dirty (read more about AddHandler/RemoveHandler, References, …)
  • Method MyHeaderBackgroundPaint calls an own Event Definition

That way you can implement the Event MyHeaderBackgroundPaint.
It won’t do anything in XojoVersions pre 2020. But it will compile with both pre/post 2020 Xojo Versions.

HeaderBackgroundPaint.xojo_binary_project

I hope that helps to get an idea of how it could be done…

Thanks Jürg, your example project works perfectly in 2020r2 and 2015r4.1 doesn’t complain about the event. This is exactly what I was looking for. I already subclass the ListBox for some other reasons so it will be easy to add. Thanks for taking the time to post an example that shows me what I was missing.

Great community here!

3 Likes