Edit

Share via


AddHandler Statement

Associates an event with an event handler at run time.

Syntax

AddHandler event, {AddressOf eventhandler | expression }

Parts

Part Description
event The name of the event to handle.
eventhandler The name of a procedure that handles the event.
expression A lambda expression that handles the event.

The parts AddressOf eventhandler and expression are mutually exclusive.

Remarks

The AddHandler and RemoveHandler statements allow you to start and stop event handling at any time during program execution.

The signature of the new event handler (the eventhandler procedure or the expression lambda) must match the signature of the event event.

The Handles keyword and the AddHandler statement both allow you to specify that particular procedures handle particular events, but there are differences. The AddHandler statement connects procedures to events at run time. Use the Handles keyword when defining a procedure to specify that it handles a particular event. For more information, see Handles.

A handler added with an explicit lambda CANNOT be removed later (using RemoveHandler). Indeed, if the lambda is not given a name, it is not possible to reference it later. But assigning the lambda to a variable and adding the handler through this variable allows to remove the handler using this variable.

Note

For custom events, the AddHandler statement invokes the event's AddHandler accessor. For more information on custom events, see Event Statement.

Example

The following example demonstrates how to use AddHandler with ConvertEventHandler delegates for data binding scenarios. This example shows a practical use case where event handlers are attached to Format and Parse events of a Binding object to convert between decimal values and currency strings.

Public Class DataBindingExample
    Private textBox1 As TextBox
    Private ds As DataSet
    
    Public Sub New()
        textBox1 = New TextBox()
        ds = New DataSet()
        SetupSampleData()
        BindControlWithAddHandler()
    End Sub
    
    Private Sub SetupSampleData()
        Dim table As New DataTable("Orders")
        table.Columns.Add("OrderAmount", GetType(Decimal))
        table.Rows.Add(123.45D)
        table.Rows.Add(67.89D)
        ds.Tables.Add(table)
    End Sub
    
    Private Sub BindControlWithAddHandler()
        Dim binding As New Binding("Text", ds, "Orders.OrderAmount")
        
        ' Use AddHandler to associate ConvertEventHandler delegates
        AddHandler binding.Format, AddressOf DecimalToCurrency
        AddHandler binding.Parse, AddressOf CurrencyToDecimal
        
        textBox1.DataBindings.Add(binding)
    End Sub
    
    Private Sub DecimalToCurrency(ByVal sender As Object, ByVal e As ConvertEventArgs)
        If e.DesiredType IsNot GetType(String) Then
            Return
        End If
        e.Value = CDec(e.Value).ToString("c")
    End Sub
    
    Private Sub CurrencyToDecimal(ByVal sender As Object, ByVal e As ConvertEventArgs)
        If e.DesiredType IsNot GetType(Decimal) Then
            Return
        End If
        e.Value = Convert.ToDecimal(e.Value.ToString())
    End Sub
End Class

' Simple example for basic AddHandler usage
Sub TestBasicEvents()
    Dim Obj As New Class1
    AddHandler Obj.Ev_Event, AddressOf EventHandler
    Obj.CauseSomeEvent()
    RemoveHandler Obj.Ev_Event, AddressOf EventHandler
    Obj.CauseSomeEvent()
    
    ' Lambda expression example
    AddHandler Obj.Ev_Event, Sub ()
        MsgBox("Lambda caught event.")
    End Sub
    Obj.CauseSomeEvent()
End Sub

Sub EventHandler()
    MsgBox("EventHandler caught event.")
End Sub

Public Class Class1
    Public Event Ev_Event()
    Sub CauseSomeEvent()
        RaiseEvent Ev_Event()
    End Sub
End Class

This example shows:

  • Creating a Binding object for data binding.
  • Using AddHandler to attach ConvertEventHandler delegates to the Format and Parse events.
  • Implementing event handler methods that convert between decimal and currency string formats.
  • Basic AddHandler usage with custom events and lambda expressions.

See also