请教高手一个算法,遍历页面中所有的TEXTBOX的控件,并且.TEXT属性初始化为“1”?

来源:百度知道 编辑:UC知道 时间:2024/06/17 18:56:06
应该思路是这样

可是不行啊,为什么

foreach (Control c in Controls)
{
if (c is TextBox )
{
((TextBox)c).Text = "asd";
}

}
啊,递归怎么搞呢高手

你将所有的TextBox放在form表单中,直接遍历就行了

foreach(Control c in this.form.controls)
{
if(c is textbox)
{
(c as Textbox).Text="1";
}
}

foreach(control ctl in this.controls)
{
if(ctl is textbox || ctl is combox)
ctl.text=string.empty;
}

需要递归,因为 textbox的父控件,不一定是 FORM 或 PAGE本身,而是 PANEL或 GROUPBOX 之类的

你的代码,只遍历了最上层,所以需要递归遍历