如何在C#中编写动态pictureBox Click事件?

时间:2016-01-29 08:03:59

标签: c#

private void button1_Click(object sender, EventArgs e)
{
        PictureBox dynamicPicture1 = new PictureBox();
        dynamicPicture1.Tag = i;
        dynamicPicture1.Location = new Point(x, y);
        dynamicPicture1.Name = "pictureBox" + i;
        dynamicPicture1.Size = new System.Drawing.Size(30, 27);
        dynamicPicture1.ImageLocation = 
            "C:\\Users\\Newfolder\\Downloads\\x`enter code here`ryrvc.jpg";
        panel1.Controls.Add(dynamicPicture1);

}

2 个答案:

答案 0 :(得分:1)

您应该设置Method的{​​{1}}名称:

event handler

并在某处定义事件处理程序:

dynamicPicture1.Click += dynamicPicture1_Click; //note the name here

事件处理程序的名称必须相互匹配...

答案 1 :(得分:1)

尝试此更新代码。

void dynamicPicture1_Click(object sender, EventArgs e) {
    throw new NotImplementedException(); //default not implemented
}