0

Screen of my user control

I have an user control (black rectangle) that contains some controls. When the mouse is hover the user control, I want to show the two images in the red rectangle.

At the moment I have implemented the mouseEnter (show the images) and mouseLeave (hide the images) events from the user control.

The problem is that when the mouse is hover another control (a label for instance), the mouseLeave event from the user control is fired and the images disappear.

I could implement the mouse enter / mouse leave events for all the controls but it seems a waste of time, lots of code deduplication and a poor usage of memory. I also thought about disable events for the other controls, but not sure how to do it and it seems to be more of a hack than anything. Il tried to disable the controls but the color changes and again, a dirty hack.

Maybe I can prevent the user control mouseLeave event to be fired if the mouse is hover one of its control ?

I'm sure there is a proper way to do it, but cannot figure it out.

If anyone thinks about a better title, I will change it.

Thank you for your help,

Regards

  • If it's Windows Forms, why don't you just enumerate `mainUserControl.Controls` and check if any are `Focused` in the events? I.e. in the `mouseLeave` event, if any children are focused don't trigger the change. – Der Kommissar May 16 '17 at 17:55
  • This [thread](http://stackoverflow.com/questions/4991044/winforms-intercepting-mouse-event-on-main-form-first-not-on-controls) might be useful for what you are trying to do. – Sharada Gururaj May 16 '17 at 17:56

1 Answers1

0

Thank you very much both of you. You pointed me to the right direction.

I ended up to simply register the ControlUser.MouseEnter event to all controls by doing :

foreach (Control control in Controls)
{
     control.MouseEnter += new EventHandler(this.MyUserControl_MouseEnter);
}

Simple and efficient, this is perfect.