动态控件和事件 [英] Dynamic Controls and Events

查看:64
本文介绍了动态控件和事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


 我有一段代码,可根据图像列表中的图像数量在运行时将控件添加到表单中:

用于 ( int i2 = 0; i2< il.Images.Count; i2 ++)
{
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 =
点(10,( int )thumbHeight * i2 +30) ;
}
其他
{
    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.

The

private 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆