2015-12-30 79 views
-1

首先,我已將this link的代碼複製到任何效果。我無法檢測Visual Studio 2015中的右鍵單擊事件

我正在嘗試處理左鍵單擊和右鍵單擊按鈕。左鍵點擊註冊並正確執行,右鍵點擊根本沒有效果。我相信相關代碼如下:

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

你想使用MouseDown事件而不是Click。此外,你必須在小組的名稱拼寫錯誤 - 它應該Button_MouseDown而不是Button_ 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 
+0

這是它,謝謝! – coinich