Search This Blog

Thursday, December 5, 2013

AddHandler Visual Basic Example

VB.NET > Statements > AddHandler

AddHandler link an event with an event handler at run time.

Example:

1. Add event to an object

Public Class Form1
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim Obj As New Class1
        ' Associate an event handler with an event
        AddHandler Obj.myEvent, AddressOf EvHandler
        Obj.Class1Event()
    End Sub

    Sub EvHandler()
        ' Handle the event
        MsgBox("EvHandler")
    End Sub

    Public Class Class1
        Public Event myEvent()
        Public Sub Class1Event()
            RaiseEvent myEvent()
        End Sub
    End Class

End Class

2. Handle events for dynamic runtime control