【问题标题】:Different Panels' MouseEnter and MouseLeave conflict on Event Order不同面板的 MouseEnter 和 MouseLeave 事件顺序冲突
【发布时间】:2014-10-12 08:04:46
【问题描述】:

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

  • 如果我的光标在第一个上面 (MouseEnter)
    • BackColor 将变为黑色
    • 第二个面板可见
  • 如果我的光标离开第一个 (MouseLeave)
    • BackColor 会变回灰色
    • 第二个面板将不可见

这很简单:

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 事件中一样

下面是需要明确的场景:

这是我使之成为可能的逻辑:(使用相同的代码提供相同的事件)

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

看起来很合理,但我认为系统首先考虑first_pnlMouseLeave,然后再考虑second_pnlMouseEnter。 有什么办法吗?

【问题讨论】:

  • 我觉得你的说法有点矛盾。以及为什么在第二个面板没有工作时需要第二个面板的事件,因为您在顶部的陈述只告诉第一个面板应该发生什么,以便执行以下操作。没有你上面提到的第二个面板应该有鼠标事件,那么在你的第二个代码中为什么你在第二个面板插入处理鼠标进入和离开的事件?
  • 对不起,误解了,但我已经提到“当光标移动到第二个面板时”是的,它有鼠标事件。
  • 好的。抱歉,我只是没查清楚。
  • 您可以只处理MouseEnter 事件并使用父容器的MouseEnter 事件来执行您当前在MouseLeave 事件上执行的操作。
  • 鼠标事件总是按照enter > leave的顺序发生。所以在第二个面板引发MouseEnter 事件之前,第一个面板将引发MouseLeave 事件并隐藏第二个面板。 总结: 第二个面板永远不会引发MouseEnter 事件。

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


【解决方案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

【讨论】:

    猜你喜欢
    • 2016-04-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-14
    相关资源
    最近更新 更多