title: Winforms: How to attach an event handler to additional controls
body: <p>How do i attach the same event handler to additional controls in Winforms/.NET/C#?</p>
  
  <hr>
  
  <p>i <strong><em>randomly</em></strong> tried perfectly logical code to accomplish what i want, but unfortunately the syntax is not valid in C#:</p>
  
  <pre><code>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;
  }
  </code></pre>
  
  <p>This <em>invalid</em> code causes the <strong><em>syntax error</em></strong>:</p>
  
  <blockquote>
    <p>The event 'System.Windows.Forms.MouseEnter' can only appear on the left hand side of a += or -=</p>
  </blockquote>
  
  <p>In this example i want the Panel's <code>MouseEnter</code> event to fire if the mouse enter's any control in the panel.</p>
  
  <p><a href="https://stackoverflow.com/questions/8173583/winforms-how-to-attach-same-event-handler-to-additional-controls">How do i attach the same event handler to additional controls in Winforms/.NET/C#?</a></p>
  
  <p>The code i tried doesn't compile.</p>
  
  <h2>Related Questions</h2>
  
  <ul>
  <li><a href="https://stackoverflow.com/questions/8172535/winforms-how-to-cause-mouseenter-to-fire-when-the-mouse-enters-a-control">WinForms: How to cause MouseEnter to fire when the mouse enters a control?</a></li>
  <li><a href="https://stackoverflow.com/questions/8159534/net-how-to-check-if-the-mouse-is-in-a-control">.NET: How to check if the mouse is in a control?</a></li>
  </ul>