如何获得父控件的所有子? [英] How to get all children of a parent control?

查看:118
本文介绍了如何获得父控件的所有子?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一个code例如如何让家长控制的所有儿童。

我不知道如何做到这一点。

 的foreach(在控制控制控制)
{
  如果(control.HasChildren)
  {
    ?
  }
}
 

解决方案

如果您只想眼前的孩子,用

  ...
VAR孩子= control.Controls.OfType<控制>();
...
 

如果你从层级希望所有的控件(例如,一切都在树内的某个控制下),使用方法:

 私人的IEnumerable<控制> GetControlHierarchy(控制根)
    {
        VAR队列=新的队列和LT;控制>();

        queue.Enqueue(根);

        做
        {
            无功控制= queue.Dequeue();

            产量恢复控制;

            的foreach(VAR孩子control.Controls.OfType<控制>())
                queue.Enqueue(子);

        }而(queue.Count大于0);

    }
 

然后,你可能会使用类似这样的形式:

 私人无效的button1_Click(对象发件人,EventArgs的)
    {
        ///获取所有控件在一个名单,其中形式的层次;>
        VAR controlList = GetControlHierarchy(本).ToList();

        的foreach(在controlList功控制)
        {
            ///做一些与此对照
        }
    }
 

修改

我修改 GetControlHierarchy()的方法来使用一队列,而不是一个堆,这可能是更有效的,并且替换了 control.Controls.OfType<>()为清楚起见

更新

这是疯狂不可能的,你永远风与在控制层次结构中的周期,但它在技术上是可行的。如果发生这种情况,一)有人做错了,以及b)添加一个哈希表,保持控制哈希的轨道,并修改 GetControlHierarchy()方法要么扔异常或根本就没有收益率包括已经处理的任何控制权。 (这是附带在采访中,由于某种原因,事情的类型。)

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)
  {
    ??
  }
}

解决方案

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:

    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 a List<>
        var controlList = GetControlHierarchy(this).ToList();

        foreach (var control in controlList)
        {
            /// do something with this control
        }
    }

Edit

I modified the GetControlHierarchy() method to use a queue instead of a stack, which may be more efficient, and replaced the is and as with control.Controls.OfType<>() for clarity.

Update

It is insanely unlikely that you'll ever wind up with a cycle in a control hierarchy, but it is technically possible. If this happens, a) someone is doing it wrong, and b) add a hash table that keeps track of the control hashes, and modify the GetControlHierarchy() method to either throw an exception or simply not yield any control that's already been processed. (This is the type of thing that comes up in interviews, for some reason.)

这篇关于如何获得父控件的所有子?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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