2

i have a Panel which contains an arbitrary number of child controls:

enter image description here

If the mouse enters one of the child controls in the Panel:

enter image description here

Then the MouseEnter event of the Panel is not fired.

Note: A related problem is that if the mouse moves from the the panel to one of the child controls, then the Panel's MouseLeave event is fired:

enter image description here

Even though the mouse did not leave the panel.

How can i cause the MouseEnter event of a Panel to fire if the mouse enters "any" control that is a child on the panel?

Ian Boyd
  • 246,734
  • 253
  • 869
  • 1,219
  • 1
    WPF makes this easier with bubbling events, but in WinForms, you're stuck with writing plumbing code. – Davy8 Nov 17 '11 at 19:59
  • 1
    I'm wondering what makes people stick to WinForms. I have been using WPF for the past 4 and half years and never touched WinForms again since then. WPF is charming. Winforms sucks. – Shimmy Weitzhandler May 09 '13 at 03:35
  • @Shimmy The inability in WPF to [layout forms in dlus](http://stackoverflow.com/questions/7147716/how-to-specify-units-in-dialog-units-in-wpf), the inability [to specify dialog units as units](http://stackoverflow.com/questions/395195/wpf-how-to-specify-units-in-dialog-units), the requirement that forms be laid out using an XML markup language, and that you cannot not use databinding. – Ian Boyd May 10 '13 at 14:35
  • @IanBoyd you cannot use databinding was the final and best joke. i am still laughing hard!! – Shimmy Weitzhandler May 12 '13 at 00:02

1 Answers1

4

Something like:

foreach (Control ctrl in panel1.Controls)
            ctrl.MouseEnter += panel1_MouseEnter;
KreepN
  • 8,528
  • 1
  • 40
  • 58
Dan Byström
  • 9,067
  • 5
  • 38
  • 68
  • Perhaps you can answer how to have the generic code (http://stackoverflow.com/questions/8173583/winforms-how-to-attach-event-handler-to-other-controls) Short version: `control.MouseEnter += panel.MouseEnter` doesn't compile. – Ian Boyd Nov 17 '11 at 20:14
  • It gets hairy when i have a few dozen panels, and i don't know the *names* of the methods attached to each event. You'd think a computer would be able to tell you such things - rather than having to maintain such a large ball of mud. – Ian Boyd Nov 17 '11 at 21:08
  • I wrote "panel1_MouseEnter" - meaning the default event handler Visual Studio writes for you. Not "control.MouseEnter += panel.MouseEnter" - which, agreed, doesn's compile. – Dan Byström Nov 18 '11 at 07:56
  • One possible solution would be to create a UserControl which takes care of this and use that instead of the standard Panel. "PanelWhichFiresMouseEnterForItsChildren" :-) – Dan Byström Nov 18 '11 at 07:58