为动态创建的面板添加鼠标点击事件

问题描述:

我正在用 C# 开发 Windows 窗体应用程序.

Hi i am developing a windows forms application in C#.

我有一个 FlowLayoutPanel,里面有一个动态的面板列表.如何为 FlowLayoutPanel 内的面板添加鼠标点击事件?

I have a FlowLayoutPanel and inside it i have a dynamic list of panels. How can i add mouse click events for the panels inside FlowLayoutPanel?

感谢这个.但是我可以调用一些空函数,而不是事件函数.类似这样的:

Thx for this.But can i call some void function, and not event funtion.Something like this:

private void example(String x)
    {
        label2.Text = x;
    }

你可以这样做:

private void attachClickEventHandler()
{
  for (int i = 0; i < 10; i++)
  {
     Panel p = new Panel();
     p.Click+=p_Click;
     flowLayoutPanel1.Controls.Add(p);
  }

// OR

  foreach(Control c in flowLayoutPanel1.Controls)
     if(c is Panel)
        c.Click += p_Click;
}

void p_Click(object sender, EventArgs e)
{
   // do click stuff
}