Different Panels' MouseEnter and MouseLeave conflict on Event Order

試著忘記壹切 提交于 2019-12-24 04:29:30

问题


Here, I have two side-by-side panel the first_pnl and second_pnl, the second panel is not visible by default. Initial thoughts of what I need:

  • If my cursor is over the the first one (MouseEnter)
    • It's BackColor would change to Black
    • Second panel would be visible
  • If my cursor leaves first one (MouseLeave)
    • It's BackColor would change back to Gray
    • Second panel would not be visible

Which is simple as:

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

But what I want to happen is:

  • When the cursor moves to the second panel (which by now is visible)
    • second_pnl would remain visible unless the cursor leave its area.
    • It sustains first_pnl property as if it where on a MouseEnter event

And here's the scenario to be clear:

And here's my logic of making that possible: (Giving their same events with same code)

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

Looks reasonable, but I think the system consider first the MouseLeave of first_pnl before it even consider the MouseEnter of second_pnl. Any way to do it?


回答1:


jmcilhinney's comment solves this easily.

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


来源:https://stackoverflow.com/questions/26323105/different-panels-mouseenter-and-mouseleave-conflict-on-event-order

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!