在C#中获取Flowlayoutpanel的所有子级 [英] Getting all children of Flowlayoutpanel in C#

查看:960
本文介绍了在C#中获取Flowlayoutpanel的所有子级的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Flowlayoutpanel,孩子的数量未知.我该如何找回所有孩子?到目前为止,我还没有成功尝试过此操作:

I have a Flowlayoutpanel with an unknown amount of children. How can I retrieve all the children? So far I have tried this with no success:

private LoopReturn(Control control)
{    
    var c = control;

    var list = new List<Control>();

    while (c != null)
    {
        list.Add(c);
        c = c.GetNextControl(c, true);                    
    }

    foreach (var control1 in list)
        Debug.Print(control.Name);  
}

但是我只得到前两个孩子.有人知道为什么吗?

But I'm only getting the first two children. Does anybody know why?

我需要所有孩子的所有孩子的孩子,等等.

I need to the children of all children of all children etc.

推荐答案

这是怎么回事:

var controls = flowLayoutPanel.Controls.OfType<Control>();

请记住,这是线性的,不是分层的,就像您当前的算法一样.

Bear in mind that this is linear, not hierarchical, just like you're current algorithm.

要获得所有孩子,无论级别高低,您都需要执行以下操作:

To get the all children, regardless of level, you'd need to do something like this:

private IEnumerable<Control> GetChildren(Control control = null)
{
    if (control == null) control = flowLayoutPanel;

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

    foreach (var child in list)
        list.AddRange(GetChildren(child));

    return list;
}

,然后在需要总体列表时执行以下操作:

and then when you want the overall list just do this:

var controls = GetChildren();

这篇关于在C#中获取Flowlayoutpanel的所有子级的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆