现在的位置: 首页 > 综合 > 正文

控件名与字符串

2013年06月29日 ⁄ 综合 ⁄ 共 540字 ⁄ 字号 评论关闭

在界面有很多控件对象时,找寻正确的控件,然后对控件操作,是经常需要做的,在此,c#提供了一种方法:

1.利用findControl方法(webform中自带,winform中需自己写)

int intMNo=Convert.ToInt32(dataChuankou.Substring(0,2), 16);//获取checkBox的序列号
string strMNo = "checkBox" + intMNo.ToString();//checkBox+序列号 组成控件名
CheckBox cb = (CheckBox)this.findControl(groupBox2,strMNo);//查找控件名,并生成该控件的对象。
cb.Enable=false;//使用该控件,修改控件的属性。

2.直接用Controls序列

 CheckBox cb = this.Controls["" + Int32.Parse(strMNo)] as CheckBox;//查找控件名,生成控件对象
cb.Enable=false;//使用该控件,修改属性。

其他方法还有些,大家可以自己试试。

findControl方法在这里:

http://blog.csdn.net/lllljz/article/details/7694104点击打开链接

抱歉!评论已关闭.