how do I add an eventhandler to a control dynamically?


I have created a containercontrol with one label, one text field and one button.
Then I added an array of this container controls to my window.
With a push button I’m creating a new instance of the container control and add it to the array.
In a method I’m rearranging the control and renaming it.

So far it’s working fine. I can now add these controls dynamically to my tabpanel.

Now I want to be able to delete some of the controls after I created it.
The button inside of the container control triggers a self.close.

Unfortunately I cannot trigger any methods outside of the container control because the method doesn’t exist when the control is created.
Now I want to trigger the event handler onClose when the container control is closed.

Here again it is not possible to refer to an outside method in the event handler.
So I have to add the event handler at the time of creation.

I just need to know how this may work.

Does anybody know how?


ContainerInstance = New Container
AddHandler ContainerInstance.onClose, AddressOf someMethod

The first parameter to the method must be defined AS Container.

@Tim Hare
Thank you… I will look into that.

Can I use parameters with the method?

Something like:
AddHandler Waypoint.onClose, AddressOf Recalculate(Waypoint,Waypoints,“Waypoint”,135,4)

You set the call parameters in the method.

I know that I set the parameters in the method.
The question is if it’s possible to use a method which needs more than just the object type as parameter.
I’m gonna try some versions (with parameter, with only one parameter, no parameter).

Yes. The parameters of the method must match the parameters of the event definition with the addition of the object itself.

If the event definition was:

Foo(a as integer, b as Boolean)

The method is

Bar(obj as yourClass, a as integer, b as Boolean)

And you’d add it as

AddHandler myInst.Foo, addressOf Bar

NOTE: Don’t forget to call RemoveHandler when you’re done so your objects will properly get destroyed when they are removed from the array.

Hello Greg,

thanks a lot it’s working, now.

Now I have to add the RemoveHandler to my method because I hadn’t this in mind.