0

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
4

2 回答 2

1

您需要使用MouseDown()MouseUp()事件来区分使用了哪个按钮:

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
于 2013-11-10T23:48:00.063 回答
0

我不确定问题是什么:您没有分享任何错误或错误行为。但我建议进行以下更改:

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
于 2013-11-10T23:31:23.283 回答