AddHandler 与 RemoveHandler



AddHandler用于在运行时将事件与事件处理程序相关联
RemoveHandler用于移除事件与事件处理程序之间的关联

C#:

   private void ultraTextEditor1_ControlRemoved(object sender, ControlEventArgs e)
        {
            e.Control.DoubleClick -= new EventHandler(Control_DoubleClick);
        }

        private void ultraTextEditor1_ControlAdded(object sender, ControlEventArgs e)
        {
            e.Control.DoubleClick += new EventHandler(Control_DoubleClick);
 
        }

        void Control_DoubleClick(object sender, EventArgs e)
        {
            //this.ultraTextEditor1.ReadOnly = false;
            MessageBox.Show("aaa");
        }

VB.NET


    Private Sub UltraTextEditor1_ControlAdded(sender As Object, e As ControlEventArgs) Handles UltraTextEditor1.ControlAdded

        AddHandler e.Control.DoubleClick, AddressOf Control_DoubleClick
 
    End Sub

    Private Sub UltraTextEditor1_ControlRemoved(sender As Object, e As ControlEventArgs) Handles UltraTextEditor1.ControlRemoved
        RemoveHandler e.Control.DoubleClick, AddressOf Control_DoubleClick
    End Sub
 
    Sub Control_DoubleClick(ByVal sender As System.Object, ByVal e As System.EventArgs)

        MsgBox("aaa")
    End Sub

例:

    Private Sub UcButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles UcButton1.Click
        AddHandler UcButton3.Click, AddressOf MyButtonClick
    End Sub

    Private Sub UcButton2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles UcButton2.Click
        RemoveHandler UcButton3.Click, AddressOf MyButtonClick
    End Sub

    Private Sub MyButtonClick(ByVal sender As System.Object, ByVal e As System.EventArgs)
        MessageBox.Show("test")
    End Sub

你可能感兴趣的:(VB.net)