共用方式為


RemoveHandler 語句

拿掉事件與事件處理程式之間的關聯。

語法

RemoveHandler event, AddressOf eventhandler  

組件

術語 定義
event 要處理的事件名稱。
eventhandler 目前處理事件之程序的名稱。

備註

AddHandlerRemoveHandler 語句可讓您在程式執行期間隨時啟動和停止特定事件的事件處理。

備註

針對自定義事件,語句會 RemoveHandler 叫用事件的 存取 RemoveHandler 子。 如需自定義事件的詳細資訊,請參閱 Event Statement

範例

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

另請參閱