问题
Iam using windows forms. How can I query all child controls of a Form recursively which have a certain type?
In SQL you would use a selfjoin to perform this.
var result =
from this
join this ????
where ctrl is TextBox || ctrl is Checkbox
select ctrl;
Can I also do this in LINQ?
EDIT:
LINQ supports joins. Why can't I use some kind of selfjoin?
回答1:
Something like this should work (not perfect code by any means...just meant to get the idea across):
public IEnumerable<Control> GetSelfAndChildrenRecursive(Control parent)
{
List<Control> controls = new List<Control>();
foreach(Control child in parent.Controls)
{
controls.AddRange(GetSelfAndChildrenRecursive(child));
}
controls.Add(parent);
return controls;
}
var result = GetSelfAndChildrenRecursive(topLevelControl)
.Where(c => c is TextBox || c is Checkbox);
回答2:
may be this will help you...
How can I get all controls from a Form Including controls in any container?
once you have the list you can query'em
来源:https://stackoverflow.com/questions/2525062/how-can-i-query-all-childcontrols-of-a-winform-recursively