鼠标点击并按住事件?

时间:2013-10-09 21:51:20

标签: vb.net

有没有人知道如何在VB.NET中处理鼠标点击并保持事件?

假设我想单击并按住按钮并在代码后面执行一些操作,例如更改按钮BackColor或更快地关闭窗口(就像在ms office 2013文件菜单中单击时那样使用左箭头关闭那个菜单。)

希望你知道我的意思

谢谢

2 个答案:

答案 0 :(得分:2)

您可以创建一个全局定义的计时器,该计时器在调用MouseDown时开始,然后在Mouse Up上结束。然后,您可以设置一个条件,在您认为长按一次之前需要经过多少毫秒。请参阅下面的示例代码:

Public Class Form1
Dim WithEvents timer As New Timer
Dim milliseconds As Integer


Private Sub Form1_MouseDown(sender As Object, e As MouseEventArgs) Handles MyBase.MouseDown
    timer.Start()
End Sub

Private Sub Form1_MouseUp(sender As Object, e As MouseEventArgs) Handles MyBase.MouseUp
    timer.Stop()
    Label1.Text = "Button held down for: " & milliseconds & " milliseconds"
    If milliseconds >= 10 then 'Mouse has been down for one second
      DoSomething()
    End If
End Sub

Private Sub EggTimer_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles timer.Tick
    milliseconds += 1
End Sub


End Class

答案 1 :(得分:1)

您正在寻找MouseDown