(学)递归查找窗体中全部控件

/// <summary>
        /// 通过递归将控件及子级控件信息列为树形
        /// </summary>
        /// <param name="pControl"></param>
        /// <param name="pControlName"></param>
        public void BuildControlTree(dynamic pControl, string pControlName = null)
        {
            try
            {
                string strConName = "";
                if (pControl.Name == "" && pControl.Text == "") return;
                //if (PCon is DevExpress.XtraEditors.SplitGroupPanel)
                //    strConName = PID + PCon.Text;
                //else
                //    strConName = PCon.Name == "" ? PCon.Text : PCon.Name;
                strConName = (pControlName == null ? "" : pControlName + "-") + (pControl.Name == "" ? pControl.Text : pControl.Name);
                DataRow dr = dsControlTree.DT_ControlTree.NewRow();
                dr["ID"] = strConName;
                dr["ParentID"] = pControlName;
                dr["ControlName"] = strConName;
                dr["ControlType"] = pControl.GetType().Name;
                dsControlTree.DT_ControlTree.Rows.Add(dr);
                //DicCon.Add(strConName, PCon);
                //插入其子控件
                if (pControl is GridControl)
                {
                    //循环其gridview
                    foreach (GridView gv in pControl.Views)
                    {
                        BuildControlTree(gv, strConName);
                    }
                }
                else if (pControl is GridView)
                {
                    //循环其gridcolumn
                    foreach (GridColumn gdc in pControl.Columns)
                    {
                        BuildControlTree(gdc, strConName);
                    }
                }
                else if (pControl is LayoutControl)
                {
                    return;
                }
                else if (pControl is TreeList)
                {
                    //循环其TreeListColumn
                    foreach (TreeListColumn tlc in pControl.Columns)
                    {
                        BuildControlTree(tlc, strConName);
                    }
                }
                else
                {
                    if (pControl is Control && pControl.Controls.Count > 0)
                    {
                        foreach (Control CCom in pControl.Controls)
                        {
                            BuildControlTree(CCom, strConName);
                        }
                    }
                }
            }
            catch
            { }
        }
原文地址:https://www.cnblogs.com/spymaster/p/8303399.html