views:

333

answers:

1

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

Is it possible to find all child controls of specific type using Enumerable.OfType<T>() or LINQ without writing own recursive method? Like this.

+3  A: 

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<Control> FlattenChildren(this Control control)
{
  var children = control.Controls.Cast<Control>();
  return children.SelectMany(c => FlattenChildren(c)).Concat(children);
}
dh
Could you please provide an example of code?
abatishchev
sure, added the code
dh