我无法在Visual Studio 2015中检测到右键单击事件

时间:2015-12-30 20:50:10

标签: visual-studio events mouseevent

首先,我已将代码从this link复制到无效。

我正在尝试处理左键单击和右键单击按钮。左键单击注册并正确执行,右键单击完全没有效果。我相信相关的代码如下:

Dim Buttons As New Dictionary(Of Integer, Button)
...
' in a loop that creates each button
Dim B As New Button
AddHandler B.Click, AddressOf Button_MouseDown

Private Sub Button_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseDown
    'code to get uid

    If e.Button = MouseButtons.Left Then
        left_click(uid)
        'this works
    End If
    If e.Button = MouseButtons.Right Then
        right_click(uid)
        'this doesn't
    End If

1 个答案:

答案 0 :(得分:1)

您想要使用事件MouseDown而不是Click。此外,您在Sub的名称中有一个拼写错误 - 它应该是Button_MouseDown而不是Button_ B ouseDown

这将有效:

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    For i As Integer = 0 To 5
        Dim btn As New Button
        AddHandler btn.MouseDown, AddressOf Button_MouseDown
        btn.Left = i*10
        btn.Top = 10
        btn.Width = 10
        Me.Controls.Add(btn)
    Next
End Sub

Private Sub Button_MouseDown(sender As Object, e As MouseEventArgs)
    If e.Button = MouseButtons.Left Then
        Label1.Text = "Left Click"
    Else If e.Button = MouseButtons.Right Then
        Label1.Text = "Right Click"
    End If
End Sub