动态控件和事件 [英] Dynamic Controls and Events
问题描述
用于
我有一段代码,可根据图像列表中的图像数量在运行时将控件添加到表单中:
{
pb2 = 新 PictureBox();
thumbWidth =(il.Images [i2] .Height/il.Images [i2] .Width)* thumbHeight;
pb2.Size = 新 Size(( int )thumbWidth,( int )thumbHeight);
pb2.SizeMode = PictureBoxSizeMode.StretchImage;
pb2.Image = il.Images [i2];
pb2.Name = i2.ToString();
<字体颜色=#0000ff" size ="2">如果 (i2 == 0)
{
pb2.Location = 新
}
其他
{
pb2.Location = 新建 点(10,(( int )thumbHeight +5)* i2 +30);
}
pb2.BackColor = Color.Blue;
pb2.点击+ = 新 System.EventHandler(picButt_click);
此 .panel2.Controls.Add(pb2);
此 .Refresh();
}
这很好用.
但是,我不知道的是如何确定实际单击了哪个图片框.
私有 无效 picButt_click( 对象 发送方System.EventArgs e)
发送方和eventsargs似乎未传递控件的实际名称.
在此先感谢,
编辑.
有人可以告诉我吗?
sender变量包含对触发事件的控件的引用.
PictureBox pic =(PictureBox)发送方;
应在您的方法中工作.
Hi,
I have a piece of code that adds controls to a form at run time based on the number of images that I have in an image list:
for
(int i2=0; i2 < il.Images.Count; i2++)
{
pb2 = new PictureBox();
thumbWidth = (il.Images[i2].Height/il.Images[i2].Width) * thumbHeight;
pb2.Size = new Size((int)thumbWidth,(int)thumbHeight);
pb2.SizeMode = PictureBoxSizeMode.StretchImage;
pb2.Image = il.Images[i2];
pb2.Name = i2.ToString();
if (i2 == 0)
{
pb2.Location = new Point(10, (int)thumbHeight *i2 +30);
}
else
{
pb2.Location = new Point(10, ((int)thumbHeight +5) *i2 +30);
}pb2.BackColor = Color.Blue;
pb2.Click += new System.EventHandler(picButt_click);this.panel2.Controls.Add(pb2);
this.Refresh();
}
This works fine.
However, the thing I dont know is how to determine which picture box was actually clicked on.
Theprivate void picButt_click(object sender, System.EventArgs e)
sender and eventsargs doesn't seem to pass the actual name of the control.
thanks in advance,
Ed.
Can anyone tell me?解决方案Hi,
the sender variable contains a reference to the control that fired the event.
PictureBox pic = (PictureBox) sender;
should work in your method.
这篇关于动态控件和事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!