有一个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);

解决方案 »

  1.   

    写个递归函数:private void FindCtrl( Control control )
    {
        for ( Control c in control.Controls )
        {
             // your code at here.
             
             //
             FindCtrl( c );
        }
    }然后调用这个函数:FindCtrl( this );
      

  2.   

    好 的,谢谢各位。还有个小问题请教。
    我这个是一个自定义控件的FORM1(该控件的一个控件回车触发赋值函数),他上面还有一个基控件FORM,基控件FORM上面放着FORM1,FORM2,FORM3等控件FORM.要赋值的textEdit编辑框是分布在不同的子控件FORM上的。我改怎么找所有子控件FORM上的textEdit呢?
    望赐教之。