我使用以下代码在运行时用pictureBoxes填充flowLayoutPanel
for (int i = 0; i < immageArray.Length; i++)
{
Image image = Image.FromFile(immageArray[i]);
pictureBoxArray[i] = new PictureBox();
pictureBoxArray[i].Image = image;
pictureBoxArray[i].Width = 256;
pictureBoxArray[i].Height = 256;
pictureBoxArray[i].SizeMode = PictureBoxSizeMode.Zoom;
flowLayoutPanel1.Controls.Add(pictureBoxArray[i]);
}
如何为设计时尚不存在的控件创建事件/事件?
答案 0 :(得分:1)
试试这个:
pictureBoxArray[i].MouseDown += new MouseEventHandler(pictureBox_MouseDown);
...
private void pictureBox_MouseDown(object sender, MouseEventArgs e)
{
....
}
pictureBox_MouseDown
是你的mouseDown事件处理程序,当然你不仅可以附加MouseDown的任何事件,而且可以为运行时创建的任何控件执行此操作。