How to get Available controls from winForms using c#
+1
A:
I think you mean all controls on the form.
So simply you can use Controls
property inside your form object.
foreach(Control c in this.Controls)
{
//TODO:
}
Ahmed Said
2009-03-17 07:47:59
That would have to be recursive for any child controls that are containers of other controls.
ProfK
2009-03-19 20:50:04
+2
A:
Try this method in your form. It will recursively get all controls on your form, and their children:
public static List<Control> GetControls(Control form)
{
var controlList = new List<Control>();
foreach (Control childControl in form.Controls)
{
// Recurse child controls.
controlList.AddRange(GetControls(childControl));
controlList.Add(childControl);
}
return controlList;
}
Then call it with a:
List<Control> availControls = GetControls(this);
ProfK
2009-03-19 20:55:02
+6
A:
Or, ProfK's solution in enumerable syntax:
public static IEnumerable<Control> GetControls(Control form) {
foreach (Control childControl in form.Controls) { // Recurse child controls.
foreach (Control grandChild in GetControls(childControl)) {
yield return grandChild;
}
yield return childControl;
}
}
erikkallen
2009-03-19 21:07:33