VB.Net - AddHandler

In VB.Net the AddHandler statement
The AddHandler statement is used to associate an event handler with an event at run time.
The AddHandler statement has the following syntax:

AddHandler object.Event, New EventHandler(AddressOf eventhandler) 

The first argument is the object and the name of the event (for example a control).
The second argument is an expression that evaluates to an EventHandler delegate.

You must create an instance of the class first.

Dim objButton As System.Windows.Forms.Button 
objButton = New System.Windows.Forms.Button

AddHandler objButton.Click, New EventHandler(AddressOf MyButtonClick)

Public Sub MyButtonClick( _
                ByVal sender As Object, _
                ByVal s As System.EventArgs) _
                Handles objMyButton.Click
End Sub

VB.NET lets you dynamically associate event handlers by creating a delegate for you when you call the Addhandler statement.
If you want to handle shared events or events from a class you must use AddHandler.

AddHandler Button1_Click, AddressOf myEventHandler 

Private Sub myEventHandler()
End Sub

© 2023 Better Solutions Limited. All Rights Reserved. © 2023 Better Solutions Limited TopPrevNext