How can I query all Winform children recursively?

Iam using window forms. How can I query all child elements of a form, recursively, that are of a specific type?

In SQL, you must use selfjoin to accomplish 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 self-awareness?

+4
source share
2 answers

Something like this should work (not perfect code in any way ... just to convey the idea):

 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 = GetSelfAndChildren(topLevelControl) .Where(c => c is TextBox || c is Checkbox); 
+3
source

maybe this will help you ...

How can I get all controls from a form that includes controls in any container?

after you have a list, you can request it

-1
source

Source: https://habr.com/ru/post/1305235/


All Articles