检查鼠标指针是否包含面板组件

时间:2013-07-09 17:49:43

标签: vb.net

如何查看我的mouse cursor enters any component in a panel

我盯着写这段代码来检查鼠标何时进入panel中的任何复选框,然后我意识到我不知道如何实际检查鼠标是否进入面板中的组件。

 Private Sub GenCheck()
    For Each CheckBox In datapanel1.Controls


    Next
End Sub

我该怎么做呢?

修改

我有一个想法,但我不太确定

我可以说

 Private Sub GenCheck()



       Dim cb As CheckBox
    For Each cb In datapanel1.Controls
        AddHandler cb.MouseEnter, AddressOf cb_MouseEnter
        AddHandler cb.MouseLeave, AddressOf cb_MouseLeave
    Next



End Sub
Private Sub cb_MouseEnter(sender As Object, e As EventArgs)

End Sub

Private Sub cb_MouseLeave(sender As Object, e As EventArgs)

End Sub

2 个答案:

答案 0 :(得分:1)

您可以使用MouseHover确定光标何时悬停在checkbox之上。

Private Sub CheckBox1_MouseHover(sender As Object, e As System.EventArgs) Handles CheckBox1.MouseHover        
    MsgBox("Mouse over!")
End Sub`

编辑:

我在一个带有两个复选框的表单上放置了一个面板来模仿您的要求,这是您正在寻找的内容:

Private Sub Form1_Load(sender As Object, e As System.EventArgs) Handles Me.Load
    For Each pnlCheckBox As CheckBox In Panel1.Controls
        AddHandler pnlCheckBox.MouseHover, AddressOf Me.CheckBoxMouseOver
    Next
End Sub

Private Sub CheckBoxMouseOver()
    MsgBox("Mouse over!")
End Sub

答案 1 :(得分:1)

* 完成此任务的完美方式*

 Dim con As Control

    For Each con In datapanel1.Controls
        AddHandler con.MouseEnter, AddressOf con_MouseEnter
        AddHandler con.MouseLeave, AddressOf con_MouseLeave
    Next
End Sub
Private Sub con_MouseEnter(sender As Object, e As EventArgs)

    'DO SOMETHING'
End Sub

Private Sub con_MouseLeave(sender As Object, e As EventArgs)
   'DO SOMETHING'
End Sub