[英]How to receive event on Panel Controls?
我有Form,它有1个Panel,Panel有0-N PictureBox,它是动态添加到Panel中的,但是如果我将鼠标移到PictureBox上并单击它,则不会触发任何动作。 我的意思是,当我单击Panel时,它会触发click方法,但是如何确保那些PictureBox的行为也相同?
由于图片框是动态添加的,因此您需要在代码中附加click事件的事件处理程序。
在将其添加到控件集合之前,请执行以下操作。
PictureBox pbx = new PictureBox();
pbx.Click += new EventHandler(pbx_Click);
//Now assign other properties and then add it to control collection
//panel1.Controls.Add(pbx);
private void pbx_Click(object sender, EventArgs e)
{
//handle the click event here
}
如果将PictureBoxes动态添加到面板中,则还必须添加PictureBox的Click事件。
请注意,您可以在同一处理程序中处理所有PictureBoxes Click事件:
for (int i = 0; i < 10; i++)
{
PictureBox pb = new PictureBox();
pb.Name = "pb" + i;
pb.Click +=new EventHandler(pb_Click);
this.Controls.Add(pb);
}
void pb_Click(object sender, EventArgs e)
{
PictureBox pb = (PictureBox) sender;
if (pb.Name == "pb1")
{
...
}
}
你明白了...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.