如果遇到容器,
比如
panel 则需要递归遍历
private void findcontrol(Control ctl){
foreach(Control c in ctl.Controls){
if(c is Panel)findcontrol(c); //递归
if(c is TextBox)(c as TextBox).Text = ""; //直接访问textbox组件
}
}
GroupBox 也是一个容器,也要递归遍历
所以,上面的代码应写为
private void OperateControls(Control control)
{
foreach(Control c in control.Controls)
{
if(c is Panel)
{
OperateControls(c);
}
if(c is GroupBox)
{
OperateControls(c);
}
if(c is TextBox)
{
// 它是 TextBox, 要干什么随便你
}
}
}
调用时用:
OperateControls(this);
也可以
private void FindButton(Control c)
{
if (c.Controls != null)
{
//
foreach(Control x in c.Controls)
{
if (x is TextBox)
{
sss=sss+((TextBox)x).Text;
}
if (x is DropDownList)
sss=sss+((DropDownList)x).SelectedValue;
FindButton(x);
}
}
}
调用:FindButton(this);