(c#)WinForm遍历所有控件

遍历WinForm中的所有控件,只执行一次以下语句是不行的

foreach (Control ctl in this.Controls)
{
}

这样只能遍历到第一层控件,子控件是遍历不到的,要想遍历所有存在的控件,需要递归解决

//伪代码如下

 void EnumControls(Control container)
{
   foreach(Control c in container.Controls)
   {
       //c is the child control here
       EnumControls(c);        
   }
}

//调用
EnumControls(this);

 

posted @ 2016-12-09 14:49  helloEveryBody0821  阅读(3428)  评论(0编辑  收藏  举报