How can I query all Childcontrols of a Winform recursively? [duplicate]

两盒软妹~` 提交于 2020-01-09 11:43:13

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!