1

I have a Panel with 2 buttons on it. What i'm trying to do is, when the mouse is inside the panel area then the buttons are visible, and when the mouse location is off the panel area the buttons are not visible.

I'm using the MouseEnter and MouseLeave events on the panel.

On MouseEnter i do button.Show() and on MouseLeave i do button.Hide();

This seems simple, but when the buttons are visible (the mouse is inside the panel area) they are not clickable (click events are not triggered).

Some debugging shows that the when the mouse is over the button, the events MouseEnter and MouseLeave are continuously being called.

Any suggestion on this ? This should be so easy to be done that i'm actually without ideas.

2
  • Try adding button.Enabled = true; Commented Mar 5, 2013 at 9:50
  • Forget to say that the button is being enabled is MouseEnter too. Commented Mar 5, 2013 at 9:53

2 Answers 2

4

That happens because when your mouse is over any button event MouseLeave is fired on panel, so buttons are again invisible and mouse is back on panel thus firing immediately MouseEnter and showing them back.

Fix your logic by adding some flag that checks in MouseLeave event if you are on any control that belongs to that panel, if so do not hide them.

Edit: here you have an example to fix your issue - MouseEnter and MouseLeave events from a Panel and its child controls.

Sign up to request clarification or add additional context in comments.

Comments

0

Here is the solution presented in the other topic referred by gzaxx

 protected override void OnMouseLeave(EventArgs e) { if (this.ClientRectangle.Contains(this.PointToClient(Control.MousePosition))) return; else { base.OnMouseLeave(e); } } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.