AddHandler driving me crazy

How does anyone get addHandler to work? It never works for me. Even using the exact same code from the documentation doesn’t work:

MyTimer = New Timer
MyTimer.Period = 1000
MyTimer.RunMode = Timer.RunModes.Multiple
AddHandler MyTimer.Action, AddressOf TimerAction

Autocomplete shows me these entries. Action is not among them:
Bildschirm­foto 2023-03-20 um 20.58.23

What am I missing?

.Action doesn’t show in the autofill drop down you can just type it in and it will work.

Make sure your timer is in a accessible place it will not be deconstructed by the finishing of wherever you are starting it from.

(So if its in window1.opening - maybe store it as a timer in a global property of the window/app itself)

You can right mouse click on “Timer” (after “New”) then from the context menu select “New method” > “From event” and it will create the action event with the correct signature for you.

Then
AddHandler MyTimer.Action, AddressOf TimerAction
should work after it.

p.s. the Autocomplete for this is broken for years. (as @Jeremy_Jefferies noted)

3 Likes

So this happens when I do it (it’s in a container instead of a window).

The Function you are adding with “AddressOf TimerAction” - needs to have a “sender as timer” added to its parameters. (As shown in the screenshot below.

If MyTimer already has an action assigned to it you will have to call removehandler beforehand in order to replace its action with the new function.

2 Likes

Add “sender as timer” as the arguments of the method

Too slow :slight_smile:

2 Likes

The first error still shows then…

1 Like

With how you are setting this up it will be best to delete the MyTimer from the left hand side and instead add it as a property.

Adding the Timer to the window auto creates the timer, so the MyTimer = new Timer() is not needed.
If you are doing it this way just right click on the timer and add event handler → Action and Xojo will add the event handler for you.

Either do it entirely as a property as Jeremy says, or do it through the GUI, best not doing it half and half.
If you do it as a property, remember to RemoveHandler as well when all done with it as it will fire after your window has closed.

3 Likes

Oh, that’s a great trick I was not aware of. Thanks for mentioning it!

3 Likes

FYI, this also works for Delegates.

2 Likes