怎么样通过一个对象名称的字符串 ,得到该对象。 比如:有一个按钮对象 ,它的Name=btnSave 现在怎么样通过字符串 "btnSave" 得到这个按钮对象呢?比如一个函数 Button btn = (Button)Fun("btnSave") 什么的。谢谢! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 Button btn = (Button)this.Controls.Find("Name", false)[0]; js这么实现的控件设置ID=btnSavedocument.getElementById("btnSave") 递归Find那个按钮,如果要明确知道在哪个容器里的话.直接找..不知道的话就递归吧 Button btn = (Button)this.Controls.Find("Button1", true)[0];修改为true,就会查找这个窗体上所有容器,这样可以了 Control GetControlByName(string name,Control parent){Control ctl= (Control)parent.Controls.Find("Name", false)[0];if(ctl!=null)return ctl;elseforeach(Control c in parent.Controls){if(c.Controls.Count>0)ctl=GetControlByName(name,c);if(ctl!=null)return ctl}return null;}基本思路这样,徒手写的,有错的话自己改改 是winForm, 有什么办法吗? foreach(Control c in this.Controls) { if(c.GetType()==typeof(Button)) { if(c.Name=="btnSave") { /..... } }} http://topic.csdn.net/u/20080804/08/7352bb4b-7d7f-4e0d-baad-fcf04180c091.html看看 public Button Fun(string btnName) { Button btn; foreach(Control c in this.Controls) { if(c.GetType()==typeof(Button)) { if(c.Name==btnName) { btn = (Button)c; break; } } } return btn;}我这没环境.我也没测.你自己试试吧 搞定了,可能不是很好的方法,但是有用了。 Type type =this.GetType();FieldInfo field = type.GetField("ControlName", BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance);if (field != null){ object obj = field.GetValue(this); //可以转换成最终类型}多谢各位的帮助。 请问如何在program中设置form1中的控件 这个函数中帮忙给加一句代码,在线等。 请问连接MySql数据库 关于线程和同步中Mutex类的一个问题 一个有关于treeview控件的问题 数值传递的问题,怎么才可以把List 的数组中的某个数据显示在***的位置呢?? 急求大神教诲 C# Chart Control 台风线路图 如何实现 谁能搞定这个验证码 招聘.NET高级程序员 100分!求C#版的win32API列表和说明!!! 无语了,VS2005打开个项目什么也没做就100% 网址的设置问题
控件设置ID=btnSave
document.getElementById("btnSave")
Button btn = (Button)this.Controls.Find("Button1", true)[0];
修改为true,就会查找这个窗体上所有容器,这样可以了
{
Control ctl= (Control)parent.Controls.Find("Name", false)[0];
if(ctl!=null)
return ctl;
else
foreach(Control c in parent.Controls)
{
if(c.Controls.Count>0)
ctl=GetControlByName(name,c);
if(ctl!=null)
return ctl
}
return null;
}基本思路这样,徒手写的,有错的话自己改改
是winForm, 有什么办法吗?
{
if(c.GetType()==typeof(Button))
{
if(c.Name=="btnSave")
{
/.....
}
}
}
看看
public Button Fun(string btnName)
{
Button btn;
foreach(Control c in this.Controls)
{
if(c.GetType()==typeof(Button))
{
if(c.Name==btnName)
{
btn = (Button)c;
break;
}
}
}
return btn;
}我这没环境.我也没测.你自己试试吧
搞定了,可能不是很好的方法,但是有用了。 Type type =this.GetType();
FieldInfo field = type.GetField("ControlName", BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance);
if (field != null){
object obj = field.GetValue(this); //可以转换成最终类型
}多谢各位的帮助。