foreach(Control txt in Form.Controls) { if(txt is TextBox) { ................ }
}
foreach(Control c in this.Controls) { if(c is TextBox) { ..... } }
Form1() { Foo(this.Controls); }void Foo(Control.ControlCollection ctls) { foreach (Control ctl in ctls) { if (ctl is TextBox) { // 对 TextBox 进行操作 } else if (ctl.Controls != null) Foo(ctl.Controls); // 递归遍历子控件 } }
我来写个js的 呵呵txtCount=0; obj=document.getElementByTagName("input") for (i=0;i<obj.length;i++) { if (obj.type="text") { txtCount=txtCount+1; } }
int i = 0; foreach (System.Windows.Forms.Control control in this.Controls) { if (control is System.Windows.Forms.TextBox) { i++; } } messagebox.show(i.tostring());
上面代码 写错了 if (obj.type== "text ") 少了个等于号
用这个,如果有容器的话也要遍历容器里面的TextBox控件
public void FindControl(Control.ControlCollection c) { foreach (System.Windows.Forms.Control control in c) { if (control.HasChildren) { FindControl(control.Controls); } else { if (control is System.Windows.Forms.TextBox) { System.Windows.Forms.TextBox tb = (System.Windows.Forms.TextBox)control; } } } }
foreach (System.Windows.Forms.Control control in this.Controls) { if (control is System.Windows.Forms.TextBox) {
} }
foreach (Control c in this.groupBox1.Controls) { if (c is textButton) {
.... } }
到网上找到一个较好答案了,多谢各位观注。 //---------------------------------------------------- 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, 要干什么随便你 c.Text=""; } } } //调用------------------------------------ OperateControls(this);
{
if(txt is TextBox)
{
................
}
}
{
if(c is TextBox)
{
.....
}
}
{
Foo(this.Controls);
}void Foo(Control.ControlCollection ctls)
{
foreach (Control ctl in ctls)
{
if (ctl is TextBox)
{
// 对 TextBox 进行操作
}
else if (ctl.Controls != null) Foo(ctl.Controls); // 递归遍历子控件
}
}
obj=document.getElementByTagName("input")
for (i=0;i<obj.length;i++)
{
if (obj.type="text")
{
txtCount=txtCount+1;
}
}
foreach (System.Windows.Forms.Control control in this.Controls)
{
if (control is System.Windows.Forms.TextBox)
{
i++;
}
}
messagebox.show(i.tostring());
if (obj.type== "text ")
少了个等于号
用这个,如果有容器的话也要遍历容器里面的TextBox控件
{
foreach (System.Windows.Forms.Control control in c)
{
if (control.HasChildren)
{
FindControl(control.Controls);
}
else
{
if (control is System.Windows.Forms.TextBox)
{
System.Windows.Forms.TextBox tb = (System.Windows.Forms.TextBox)control; }
}
}
}
{
if (control is System.Windows.Forms.TextBox)
{
}
}
foreach (Control c in this.groupBox1.Controls)
{
if (c is textButton)
{
....
}
}
//----------------------------------------------------
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, 要干什么随便你
c.Text="";
}
}
}
//调用------------------------------------
OperateControls(this);