如何获取父控件的所有子控件?

发布于 2024-12-22 09:31:18 字数 161 浏览 0 评论 0原文

我正在寻找一个代码示例,如何获取父控件的所有子控件。

我不知道该怎么做。

foreach (Control control in Controls)
{
  if (control.HasChildren)
  {
    ??
  }
}

I'm looking for an code example how to get all children of parent control.

I have no idea how do it.

foreach (Control control in Controls)
{
  if (control.HasChildren)
  {
    ??
  }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

长不大的小祸害 2024-12-29 09:31:18

如果您只想要直接子级,请使用

...
var children = control.Controls.OfType<Control>();
...

如果您想要层次结构中的所有控件(即树中某个控件下的所有控件),请使用非常简单的数据递归方法:

    private IEnumerable<Control> GetControlHierarchy(Control root)
    {
        var queue = new Queue<Control>();

        queue.Enqueue(root);

        do
        {
            var control = queue.Dequeue();

            yield return control;

            foreach (var child in control.Controls.OfType<Control>())
                queue.Enqueue(child);

        } while (queue.Count > 0);

    }

然后,您可以在表单中使用类似的方法:

    private void button1_Click(object sender, EventArgs e)
    {
        /// get all of the controls in the form's hierarchy in an IEnumerable<>
        foreach (var control in GetControlHierarchy(this))
        {
            /// do something with this control
        }
    }

If you only want the immediate children, use

...
var children = control.Controls.OfType<Control>();
...

If you want all controls from the hierarchy (ie, everything in the tree under a certain control), use a pretty simple data-recursive method:

    private IEnumerable<Control> GetControlHierarchy(Control root)
    {
        var queue = new Queue<Control>();

        queue.Enqueue(root);

        do
        {
            var control = queue.Dequeue();

            yield return control;

            foreach (var child in control.Controls.OfType<Control>())
                queue.Enqueue(child);

        } while (queue.Count > 0);

    }

Then, you might use something like this in a form:

    private void button1_Click(object sender, EventArgs e)
    {
        /// get all of the controls in the form's hierarchy in an IEnumerable<>
        foreach (var control in GetControlHierarchy(this))
        {
            /// do something with this control
        }
    }
抠脚大汉 2024-12-29 09:31:18

控件有一个 MyControl.Controls 集合,您可以对其执行 foreach 操作。

每个控件还有一个 Parent 属性,为您提供父控件。

如果您需要下降未知数量的级别,您可以编写递归方法。

Controls have a MyControl.Controls collection which you can do a foreach on.

Each Control also has a Parent property which gives you the parent control.

You can write a recursive method if you need to go down an unknown number of levels.

阳光下慵懒的猫 2024-12-29 09:31:18

也许它对某人有用:

public void GetControlsCollection(Control root,ref List<Control> AllControls,  Func<Control,Control> filter)
{
    foreach (Control child in root.Controls)
    {
        var childFiltered = filter(child);
        if (childFiltered != null) AllControls.Add(child);
        if (child.HasControls()) GetControlsCollection(child, ref AllControls, filter);
    }
}

这是一个递归函数,用于获取可以应用过滤器的控件集合(例如按类型)。示例:

 List<Control> resultControlList = new List<Control>();
 GetControlsCollection(rootControl, ref resultControlList, new Func<Control,Control>(ctr => (ctr is DropDownList)? ctr:null ));

它将返回 rootControl 中的所有 DropDownList 及其所有子项

Maybe it could be useful for someone:

public void GetControlsCollection(Control root,ref List<Control> AllControls,  Func<Control,Control> filter)
{
    foreach (Control child in root.Controls)
    {
        var childFiltered = filter(child);
        if (childFiltered != null) AllControls.Add(child);
        if (child.HasControls()) GetControlsCollection(child, ref AllControls, filter);
    }
}

This is recursive function to get the collection of controls with the possibility of appling filter (for expample by type). And the example:

 List<Control> resultControlList = new List<Control>();
 GetControlsCollection(rootControl, ref resultControlList, new Func<Control,Control>(ctr => (ctr is DropDownList)? ctr:null ));

It will return all DropDownLists in rootControl and his all children

夏尔 2024-12-29 09:31:18

可能过于复杂,但使用 Linq 和上面/其他地方的一些想法:

    public static IEnumerable<Control> GetAllChildren(this Control root) {
        var q = new Queue<Control>(root.Controls.Cast<Control>());

        while (q.Any()) {
            var next = q.Dequeue();
            foreach (Control c in next.Controls)
                q.Enqueue(c);

            yield return next;
        }
    }

Probably overly complicated, but using Linq and some ideas from above / other places:

    public static IEnumerable<Control> GetAllChildren(this Control root) {
        var q = new Queue<Control>(root.Controls.Cast<Control>());

        while (q.Any()) {
            var next = q.Dequeue();
            foreach (Control c in next.Controls)
                q.Enqueue(c);

            yield return next;
        }
    }
疑心病 2024-12-29 09:31:18

这是使用 Linq 的简洁递归扩展函数:

        /// <summary>
        /// Recursive function to get all descendant controls.
        /// </summary>
        public static IEnumerable<Control> GetDescendants(this Control control)
        {
            var children = control.Controls.Cast<Control>();
            return children.Concat(children.SelectMany(c => GetDescendants(c)));
        }

Here's a concise recursive extension function using Linq:

        /// <summary>
        /// Recursive function to get all descendant controls.
        /// </summary>
        public static IEnumerable<Control> GetDescendants(this Control control)
        {
            var children = control.Controls.Cast<Control>();
            return children.Concat(children.SelectMany(c => GetDescendants(c)));
        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文