How do i attach the same event handler to additional controls in Winforms/.NET/C#?
i randomly tried perfectly logical code to accomplish what i want, but unfortunately the syntax is not valid in C#:
public MainForm()
{
InitializeComponent();
FixPanelMouseEnter(pnlActionCenter);
FixPanelMouseEnter(pnlAdministrativeTools);
FixPanelMouseEnter(pnlAutoPlay);
FixPanelMouseEnter(pnlBackupAndRestore);
//...snip 49 lines...
FixPanelMouseEnter(pnlWFirewall);
FixPanelMouseEnter(pnlWLiveLanguageSettings);
FixPanelMouseEnter(pnlWUpdate);
}
private void FixPanelMouseEnter(Panel panel)
{
foreach (Control ctrl in panel.Controls)
ctrl.MouseEnter += panel.MouseEnter;
}
This invalid code causes the syntax error:
The event 'System.Windows.Forms.MouseEnter' can only appear on the left hand side of a += or -=
In this example i want the Panel's MouseEnter
event to fire if the mouse enter's any control in the panel.
How do i attach the same event handler to additional controls in Winforms/.NET/C#?
The code i tried doesn't compile.