1.遍历控件:
//gbfill.Control:需要遍历的容器
foreach (Control vControl in gbfill.Controls)
{
//需要查找的控件
if (vControl is Label)
{
//找到控件做的的操作
string lblname = vControl.Name;
Control c = ExtensionClass.FindControl(gbfill, lblname);
string[] sub = c.Text.Split(':');
string[] areaname = sub[1].Split(new string[] { "\r\n" }, StringSplitOptions.None);
areaname = areaname.Where(s => !string.IsNullOrEmpty(s)).ToArray();
int y = c.Location.Y;
int x = c.Location.X;
if (c != null)
{
str += "{" + "'" + "AreaName" + "'" + ":" + "'" + areaname[0] + "'" + "," + "'" + "Top" + "'" + ":" + "'" + y + "'" + "," + "'" + "Left" + "'" + ":" + "'" + x + "'" + "},";
}
else
{
MessageBox.Show("很遗憾,没找到控件!");
}
}
}
2.查找控件:
/// <summary>
/// 按名称查找控件
/// </summary>
/// <param name="parentControl">查找控件的父容器控件</param>
/// <param name="findCtrlName">查找控件名称</param>
/// <returns>若没有查找到返回NULL</returns>
public static Control FindControl(Control parentControl, string findCtrlName)
{
Control _findedControl = null;
if (!string.IsNullOrEmpty(findCtrlName) && parentControl != null)
{
foreach (Control ctrl in parentControl.Controls)
{
if (ctrl.Name.Equals(findCtrlName))
{
_findedControl = ctrl;
break;
}
}
}
return _findedControl;
}
//用法
Control c = FindControl(gbfill, con.Name);//gbfill:控件所在的容器;con.name:控件名称