如何使用C#从Windows窗体表单获取可用控件?
或者,ProfK的可枚举语法解决方案:
public static IEnumerableGetControls(Control form) { foreach (Control childControl in form.Controls) { // Recurse child controls. foreach (Control grandChild in GetControls(childControl)) { yield return grandChild; } yield return childControl; } }
在表单中尝试此方法.它将以递归方式获取表单上的所有控件及其子项:
public static ListGetControls(Control form) { var controlList = new List (); foreach (Control childControl in form.Controls) { // Recurse child controls. controlList.AddRange(GetControls(childControl)); controlList.Add(childControl); } return controlList; }
然后用:
ListavailControls = GetControls(this);