Method to register method to be called when event is raised

Posted by zaidwaqi on Stack Overflow See other posts from Stack Overflow or by zaidwaqi
Published on 2010-06-01T17:18:22Z Indexed on 2010/06/01 17:33 UTC
Read the original article Hit count: 131

Filed under:
|

I have a Panel which contains 20 PictureBox controls. If a user clicks on any of the controls, I want a method within the Panel to be called.

How do I do this?

public class MyPanel : Panel
{
   public MyPanel()
   {
      for(int i = 0; i < 20; i++)
      {
         Controls.Add(new PictureBox());
      }
   }

   // DOESN'T WORK.
   // function to register functions to be called if the pictureboxes are clicked.
   public void RegisterFunction( <function pointer> func )
   {
        foreach ( Control c in Controls )
        {
             c.Click += new EventHandler( func );
        }
   }
}

How do I implement RegisterFunction()? Also, if there are cool C# features that can make the code more elegant, please share.

© Stack Overflow or respective owner

Related posts about c#

Related posts about winforms