Parent Control Mouse Enter/Leave Events With Child Controls
- by Paul Williams
I have a C# .NET 2.0 WinForms app. My app has a control that is a container for two child controls: a label, and some kind of edit control. You can think of it like this, where the outer box is the parent control:
+---------------------------------+
| [Label Control] [Edit Control] |
+---------------------------------+
I am trying to do something when the mouse enters or leaves the parent control, but I don't care if the mouse moves into one of its children. I want a single flag to represent "the mouse is somewhere inside the parent or children" and "the mouse has moved outside of the parent control bounds".
I've tried handling MouseEnter and MouseLeave on the parent and both child controls, but this means the action begins and ends multiple times as the mouse moves across the control. In other words, I get this:
Parent.OnMouseEnter (start doing something)
Parent.OnMouseLeave (stop)
Child.OnMouseEnter (start doing something)
Child.OnMouseLeave (stop)
Parent.OnMouseEnter (start doing something)
Parent.OnMouseLeave (stop)
The intermediate OnMouseLeave events cause some undesired effects as whatever I'm doing gets started and then stopped. I want to avoid that.
I don't want to capture the mouse as the parent gets the mouse over, because the child controls need their mouse events, and I want menu and other shortcut keys to work.
Is there a way to do this inside the .NET framework? Or do I need to use a Windows mouse hook?