Search code examples
vb.netmouseclick-event

Decrease and increase value for button right-/left-click


I got a little problem with some functionallity for for some buttons in visual basic.

What i want is that the value (text) increase by 1 on left-Mouseclick and decrease by 1 on rightMouseclick Code:

Private Sub buttons_Click(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles button1.Click, button2.Click.....
Dim this_button As Button = CType(sender, Button)
(...)
If e.Button = Windows.Forms.MouseButtons.Left Then
this_button.Text = Trim(Str(CInt(this_button.Text) + 1))
ElseIf e.Button = Windows.Forms.MouseButtons.Right Then
this_button.Text = Trim(Str(CInt(this_button.Text) - 1))
End If

Solution

  • You need to use either the MouseDown() or MouseUp() event to differentiate which button was used:

    Private Sub buttons_Click(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles button1.MouseDown, button2.MouseDown, ....
        Dim this_button As Button = CType(sender, Button)
        (...)
        If e.Button = Windows.Forms.MouseButtons.Left Then
            this_button.Text = Trim(Str(CInt(this_button.Text) + 1))
        ElseIf e.Button = Windows.Forms.MouseButtons.Right Then
            this_button.Text = Trim(Str(CInt(this_button.Text) - 1))
        End If
    End Sub