按钮右键/左键单击减少并增加值

时间:2013-11-10 23:22:13

标签: vb.net mouseclick-event

对于visual basic中的某些按钮,我遇到了一些功能问题。

我想要的是左侧鼠标点击值(文本)增加1,右侧鼠标点击减少1点 代码:

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

2 个答案:

答案 0 :(得分: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

答案 1 :(得分: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