问题
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
- It's
- If my cursor leaves first one (
MouseLeave
)- It's
BackColor
would change back to Gray - Second panel would not be visible
- It's
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 aMouseEnter
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