I am trying to find an elegant way to get controls on a Windows Forms form by name. For example:
MyForm.GetControl \"MyTextBox\"
...
But t
I don't think you can create recursive linq queries directly, but you could create a recursive method using linq:
public IEnumerable FlattenHierarchy(this Control c)
{
return new[] { c }.Concat(c.Controls.Cast().SelectMany(child => child.FlattenHierarchy()));
}
This should return a sequence containing every control contained in a control hierarchy. Then finding a match is straightforward:
public Control FindInHierarchy(this Control control, string controlName)
{
return control.FlattenHierarchy().FirstOrDefault(c => c.Name == controlName);
}
Personally I'd avoid using linq in this way though.