for (int i = 0; i < this.Controls.Count; i++)
{
foreach (System.Web.UI.Control control in this.Controls[i].Controls )
{
if (control is TextBox)
(control as TextBox).Text = "";
}
}
或者
foreach(Control cl in this.Page.FindControl("Form1").Controls)
{
if(cl.GetType().ToString()=="System.Web.UI.WebControls.TextBox")
{
((TextBox)cl).Text="";
}
}
有一个winform ,是个mdicontainer,上有很多个button,请问怎样得到这些button的集合?
------------------------------------------------------------------------------
要考虑其他的容器控件,比如说Panel
最简单的情况如下
foreach(Button b in this.Controls)
{
}
但是如果有Panel还要对Panel做一个循环
所以最好做一个递归的方法
如下:
private void FindButton(Control c)
{
if(c is Button)
{
MessageBox.Show("Button!");
}
if (c.Controls != null)
{
foreach(Control x in c.Controls)
{
FindButton(x);
}
}
}
在窗体里面调用
this.FindButton(this);