-1

First of all, I've copied the code from this link to no effect.

I am attempting to handle both left clicks and right clicks on a button. Left clicks register and properly execute, right clicks have no effect at all. I believe the relevant code is below:

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
Community
  • 1
  • 1
coinich
  • 1
  • 1

1 Answers1

1

You want to use the event MouseDown not Click. Also, you have a typo in the name of the Sub - it should be Button_MouseDown and not Button_BouseDown

This will work:

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
chijos
  • 101
  • 1
  • 4