如何将事件处理程序分配给以编程方式创建的图片框

How to assign an event handler to a programmatically created picturebox

如何将事件处理程序分配给在 Windows 表单应用程序 C# 中以编程方式创建的 PictureBox? 编辑:

我试过这个,它给出了错误

no Overload pictureBox_MouseDown

this.ListFrameImage[i].MouseDown += new EventHandler(this.pictureBox_MouseDown);

private void pictureBox_MouseDown(object sender, MouseEventArgs e)
{

    someX = e.X;
    someY = e.Y;
    drag = true;
}

只需使用 += 运算符添加一个事件处理程序:

pictureBox.MouseClick += new MouseEventHandler(your_event_handler);

或者:

pictureBox.MouseClick += new MouseEventHandler((o, a) => code here);

输入 += 后按 Tab 将生成处理程序。或者你可以写它 手动 :

pictureBox.MouseClick += pictureBox_MouseClick;

void pictureBox_MouseClick(object sender, MouseEventArgs e)
{
   // blah
}