RemoveHandler 语句

删除事件和事件处理程序之间的关联。

语法

RemoveHandler event, AddressOf eventhandler  

组成部分

术语 定义
event 正在处理的事件的名称。
eventhandler 当前处理事件的过程的名称。

注解

使用 AddHandlerRemoveHandler 语句,可以在程序执行过程中随时启动和停止特定事件的事件处理。

注意

对于自定义事件,RemoveHandler 语句调用事件的 RemoveHandler 访问器。 有关自定义事件的详细信息,请参阅事件语句

示例

Sub TestEvents()
    Dim Obj As New Class1
    ' Associate an event handler with an event.
    AddHandler Obj.Ev_Event, AddressOf EventHandler
    ' Call the method to raise the event.
    Obj.CauseSomeEvent()
    ' Stop handling events.
    RemoveHandler Obj.Ev_Event, AddressOf EventHandler
    ' This event will not be handled.
    Obj.CauseSomeEvent()
    ' Associate an event handler with an event, using a lambda.
    ' This handler cannot be removed.
    AddHandler Obj.Ev_Event, Sub ()
        MsgBox("Lambda caught event.")
    End Sub
    ' This event will be handled by the lambda above.
    Obj.CauseSomeEvent()
End Sub

Sub EventHandler()
    ' Handle the event.
    MsgBox("EventHandler caught event.")
End Sub

Public Class Class1
    ' Declare an event.
    Public Event Ev_Event()
    Sub CauseSomeEvent()
        ' Raise an event.
        RaiseEvent Ev_Event()
    End Sub
End Class

另请参阅