次の方法で共有


RemoveHandler ステートメント

イベントとイベント ハンドラーの間の関連付けを削除します。

構文

RemoveHandler event, AddressOf eventhandler  

部品

任期 定義
event 処理されるイベントの名前。
eventhandler 現在イベントを処理しているプロシージャの名前。

注釈

AddHandlerおよびRemoveHandlerステートメントを使用すると、プログラムの実行中に、特定のイベントのイベント処理をいつでも開始および停止できます。

カスタム イベントの場合、 RemoveHandler ステートメントはイベントの RemoveHandler アクセサーを呼び出します。 カスタム イベントの詳細については、「 イベント ステートメント」を参照してください。

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

こちらも参照ください