事件顺序上的不同面板的MouseEnter和MouseLeave冲突

时间:2014-10-12 08:04:46

标签: vb.net events event-handling mouseevent mouseover

在这里,我有first_pnlsecond_pnl两个并排面板,默认情况下第二个面板不可见。我需要的初步想法:

  • 如果我的光标位于第一个光标(MouseEnter)之上
    • 它的BackColor会改为黑色
    • 第二个面板可见
  • 如果我的光标离开第一个(MouseLeave
    • 它的BackColor会改回Gray
    • 第二个小组不可见

这很简单:

Private Sub PanelMouseEnter(sender As Object, e As EventArgs) _
        Handles first_pnl.MouseEnter
        first_pnl.BackColor = Color.Black
        second_pnl.Visible = True
    End Sub
    Private Sub PanelMouseLeave(sender As Object, e As EventArgs) _
        Handles first_pnl.MouseLeave
        first_pnl.BackColor = Color.Gray
        second_pnl.Visible = False
    End Sub

但我想要发生的是:

  • 当光标移动到第二个面板(现在可见)
      除非光标离开其区域,否则
    • second_pnl将保持可见。
    • 它支持first_pnl属性,就像它在MouseEnter事件
    • 上的位置一样

这里的情况很清楚: Scenarios

这就是我使这成为可能的逻辑:(用相同的代码给出相同的事件)

Private Sub PanelMouseEnter(sender As Object, e As EventArgs) _
        Handles first_pnl.MouseEnter, second_pnl.MouseEnter
        first_pnl.BackColor = Color.Black
        second_pnl.Visible = True
    End Sub
    Private Sub PanelMouseLeave(sender As Object, e As EventArgs) _
        Handles first_pnl.MouseLeave, second_pnl.MouseLeave
        first_pnl.BackColor = Color.Gray
        second_pnl.Visible = False
    End Sub

看起来很合理,但我认为系统在考虑MouseLeave的{​​{1}}之前,首先考虑first_pnl MouseEnter。 有办法吗?

1 个答案:

答案 0 :(得分:1)

jmcilhinney's comment很容易解决这个问题。

Private Sub Form1_MouseEnter(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.MouseEnter
    first_pnl.BackColor = Color.Gray
    second_pnl.Visible = False
End Sub

Private Sub first_pnl_MouseEnter(ByVal sender As Object, ByVal e As System.EventArgs) Handles first_pnl.MouseEnter
    first_pnl.BackColor = Color.Black
    second_pnl.Visible = True
End Sub