简体   繁体   English

运行时创建的控件中的事件

[英]Events from controls created at run-time

I am populating a flowLayoutPanel with pictureBoxes at run-time with the following code 我在运行时使用以下代码用图片框填充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]);
        }

How can do I create the event/events for controls that don't exist yet at design time? 如何在设计时为尚不存在的控件创建事件?

Try this: 尝试这个:

pictureBoxArray[i].MouseDown += new MouseEventHandler(pictureBox_MouseDown);

...

private void pictureBox_MouseDown(object sender, MouseEventArgs e) 
{  
    ....
}

pictureBox_MouseDown is your mouseDown event handler, of course you can attach any event not only MouseDown and you can do it for any control created at runtime. pictureBox_MouseDown是mouseDown事件处理程序,当然,您不仅可以附加任何事件,还可以附加MouseDown,并且可以对运行时创建的任何控件进行附加操作。

here is the list of events for PictureBox 这是PictureBox事件列表

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM