A "function pointer" is represented by a delegation type in C #. The event Clickexpects a delegate of type EventHandler. Therefore, you can simply pass EventHandlerthe RegisterFunction method and register it for each Click event:
public void RegisterFunction(EventHandler func)
{
foreach (Control c in Controls)
{
c.Click += func;
}
}
Using:
public MyPanel()
{
for (int i = 0; i < 20; i++)
{
Controls.Add(new PictureBox());
}
RegisterFunction(MyHandler);
}
, EventHandler , PictureBox ( ). , , , PictureBox:
public MyPanel()
{
for (int i = 0; i < 20; i++)
{
PictureBox p = new PictureBox();
p.Click += MyHandler;
Controls.Add(p);
}
}
, EventHandler, :
private void MyHandler(object sender, EventArgs e)
{
}