Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Removes the association between an event and an event handler.
Syntax
RemoveHandler event, AddressOf eventhandler
Parts
| Term | Definition |
|---|---|
event |
The name of the event being handled. |
eventhandler |
The name of the procedure currently handling the event. |
Remarks
The AddHandler and RemoveHandler statements allow you to start and stop event handling for a specific event at any time during program execution.
Note
For custom events, the RemoveHandler statement invokes the event's RemoveHandler accessor. For more information on custom events, see Event Statement.
Example
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