您的位置:首页 > 其它

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

2018-01-17 16:00 323 查看
/// <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
{ }
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: