如何检查我的鼠标是否在Datagridview中?

时间:2013-06-14 08:48:16

标签: .net vb.net winforms datagridview mouseevent

我想检查我的鼠标是否在datagridview内部/外部。如果我把鼠标放在外面,它应该运行我的计时器代码,在它隐藏datagridview之前开始倒计时(在我的例子中为2秒)..如果它在里面,计时器应该将我的计数器重置为0以便不隐藏我的datagridview ..

下面是我的计时器代码.. w / 100间隔

Private ctme As Integer = 0
    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        If DataGridView1.Visible = False Then ctme = 0 : Exit Sub
        If Not DataGridView1.Bounds.Contains(PointToClient(Cursor.Position)) Then
            ctme = ctme + 1
            If ctme >= 20 Then
                ctme = 0
                DataGridView1.Visible = False
            End If
        Else
            ctme = 0
        End If
    End Sub

我试过了

Datagridview1.ClientRectangle.Contains(PointtoClient(Cursor.Position))

取代

Datagridview1.Bounds.Contains(PointtoClient(Cursor.Position))

但它仍无法正常工作..

希望你能帮我解决这个问题。

2 个答案:

答案 0 :(得分:1)

您可以使用MouseEnter(当鼠标指针进入控件时发生)和MouseLeave(当鼠标指针离开控件时发生)事件:

使用此解决方案更简单:

Private Sub DataGridView1_MouseEnter(sender As Object, e As EventArgs) Handles DataGridView1.MouseEnter
    Timer1.Stop()
End Sub

Private Sub DataGridView1_MouseLeave(sender As Object, e As EventArgs) Handles DataGridView1.MouseLeave
    Timer1.Start()
End Sub

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
    Timer1.Interval = 2000
End Sub

Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
    DataGridView1.Visible = False
End Sub

答案 1 :(得分:-1)

每个事件:

<强>的MouseEnter

Timer1.Stop()

<强>鼠标离开

 Timer1.Start()

<强> Form1_Load的

Timer1.Interval = 2000

<强> Timer1.Tick

DataGridView1.Visible = False