asp教程.net 生成控件后自动获取控件的name值
//用控件的tag属性比较方便
private void message(object sender,eventargs e)
{
picturebox pic = sender as picturebox;
string lblname=((label)pic.tag).name;
messagebox.show(lblname);
}
//动态生成控件时,指定name,然后找到它:foreach (control ct in form1.controls)
{
//c# 只遍历窗体的子控件,不遍历孙控件
//当窗体上的控件有子控件时,需要用递归的方法遍历,才能全部列出窗体上的控件
if (ct.name=="")
{
}
}
}
//实例
picturebox p = new picturebox();
p.name = "p" + i.tostring();
p.sizemode = system.windows.forms.pictureboxsizemode.stretchimage;
p.imagelocation = @"d:www.111cn.nethome4.png";
p.size = new system.drawing.size(60, 60);
p.location = new system.drawing.point(5 * 17 * i-60, 50);
p.cursor = system.windows.forms.cursors.hand;
p.doubleclick += new eventhandler(message);
p.mousemove += new system.windows.forms.mouseeventhandler(this.mouseo教程ver);
p.mouseleave += new system.eventhandler(this.mouseo教程ut);
p.contextmenustrip = contextmenustrip1;
this.sp_l_r.panel2.controls.add(p);
label la = new label();
la.name = "la" + i.tostring();
la.text = "0000";
la.location = new system.drawing.point(50, 120);
this.sp_l_r.panel2.controls.add(la);