如何删除从控件集

时间:2016-08-05 02:40:44

标签: vb.net

我有以下错误消息

  

' AddressOf'操作数必须是方法的名称(没有括号)

尝试以下代码时:

Private Sub Button1_Click(ByVal sender As System.Object, 
                          ByVal e As System.EventArgs) Handles Button1.Click
    Dim xbutton As Control

    For Each xbutton In gcFRightMenu.Controls
        If xbutton.Name = commandbutton(0) Then

            RemoveHandler xbutton.MouseDown, AddressOf xbutton.MouseDown

            AddHandler xbutton.MouseDown, AddressOf _MouseDown

        End If
    Next
End Sub


Private Sub _MouseDown(ByVal sender As Object, 
                       ByVal e As System.Windows.Forms.MouseEventArgs)
    If e.Button = Windows.Forms.MouseButtons.Left Then SendKeys.Send("{ESC}")
End Sub

1 个答案:

答案 0 :(得分:1)

您无法删除活动

RemoveHandler xbutton.MouseDown, AddressOf xbutton.MouseDown

AddressOf期待该方法的名称,但xbutton.MouseDown是事件的名称。

如果您想确保eventhandler _MouseDown仅向控件添加了一个,则在添加

之前始终将其删除
For Each xbutton In gcFRightMenu.Controls
    If xbutton.Name = commandbutton(0) Then

        RemoveHandler xbutton.MouseDown, AddressOf _MouseDown

        AddHandler xbutton.MouseDown, AddressOf _MouseDown

    End If
Next