积极答复者
关于遍历 窗体中的控件的问题 就差一句代码不会

问题
-
如下代码可以提供遍历Controls的功能,这几句代码的意义我都注释好了。
string mytext;
foreach(Control co in this.Controls) //1。遍历控件
{
mytext=co.GetType().ToString(); //2。得到当前控件的类型
if(mytext=="System.Windows.Forms.TextBox") //3。如果当前控件的类型是TextBox
((TextBox)co).Text=""; //4 。把当前控件co强制转换成TextBox类型。并把Text属性赋值为空。
}
但是我需要的功能是1,遍历控件 2得到当前控件的类型,3把当前控件强制转换成第二步得到的类型。 也就是最后的第三步 不知道怎么代码实现,烦请各位大大不吝赐教
答案
-
oreach(Control co in this.Controls) //1。遍历控件
{
var txt= co as TextBox;if (txt != null)
txt.Text = "";
}
知识改变命运,奋斗成就人生!- 已标记为答案 George.WuyazheModerator 2011年4月26日 1:02
-
if(co is TextBox)
TextBox txt=(TextBox)co;
胡超- 已标记为答案 George.WuyazheModerator 2011年4月26日 1:02
-
在补充一个写法
if(mytext.GetType() == typeof(TextBox))
{
}
2011 c# mvp China. *George读起来像不像“饺子”?我爱吃饺子,我叫George。- 已标记为答案 George.WuyazheModerator 2011年4月26日 1:03
全部回复
-
oreach(Control co in this.Controls) //1。遍历控件
{
var txt= co as TextBox;if (txt != null)
txt.Text = "";
}
知识改变命运,奋斗成就人生!- 已标记为答案 George.WuyazheModerator 2011年4月26日 1:02
-
if(co is TextBox)
TextBox txt=(TextBox)co;
胡超- 已标记为答案 George.WuyazheModerator 2011年4月26日 1:02
-
在补充一个写法
if(mytext.GetType() == typeof(TextBox))
{
}
2011 c# mvp China. *George读起来像不像“饺子”?我爱吃饺子,我叫George。- 已标记为答案 George.WuyazheModerator 2011年4月26日 1:03