Find all child controls of specific type using Enumerable.OfType() or LINQ

前端 未结 3 592
野性不改
野性不改 2020-11-30 07:39

Existed MyControl1.Controls.OfType() searches only thru initial collection and do not enters to children.

Is it possible to find all

3条回答
  •  自闭症患者
    2020-11-30 08:25

    I use an extension method to flatten control hierarchy and then apply filters, so that's using own recursive method.

    The method looks like this

    public static IEnumerable FlattenChildren(this Control control)
    {
      var children = control.Controls.Cast();
      return children.SelectMany(c => FlattenChildren(c)).Concat(children);
    }
    

提交回复
热议问题