(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);

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM