Say I have an object that I dynamically create. For example, say I create a button called "MyButton":
Dim MyButton as New Button() MyButton.Name = "MyButton"
How do I create, say, a "Click" event? If it were statically created I could create a function as:
Private Sub MyButton_Click(ByVal sender as system.object, ByVal e As System.EventArgs) Handles.
How do I implement an event handler for MyButton?
From the Class Name drop-down list at the top of the Code Editor, select the object that you want to create an event handler for. From the Method Name drop-down list at the top of the Code Editor, select the event. Visual Studio creates the event handler and moves the insertion point to the newly created event handler.
Call an event handler using AddHandler Make sure the event is declared with an Event statement. Execute an AddHandler statement to dynamically connect the event-handling Sub procedure with the event. When the event occurs, Visual Basic automatically calls the Sub procedure.
You use AddHandler
and AddressOf
like this:
Dim MyButton as New Button() MyButton.Name = "MyButton" AddHandler MyButton.Click, AddressOf MyButton_Click
There is more info here in the MSDN documentation:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With