Question

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
Was it helpful?

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

OTHER TIPS

I'm not sure what the problem is: you didn't share any error or wrong behavior. But I would suggest these changes:

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 = DirectCast(sender, Button)
    ' (...)

    Dim buttonValue As Integer = CInt(this_button.Text)
    If e.Button = Windows.Forms.MouseButtons.Left Then
        this_button.Text = (buttonValue + 1).ToString()
    ElseIf e.Button = Windows.Forms.MouseButtons.Right Then
        this_button.Text = (buttonValue - 1).ToString()
    End If
End Sub
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top