Thursday, April 21, 2011

find all descendant controls from given control without explicit recursion?

Are there some static method which can help?

From stackoverflow
  • The Control.Controls proeprty will return all child controls of the given control, but not the entire tree of child controls. But it's not hard to write a method that returns all child controls. If you are on .NET 3.5 you can use Linq to do it quite easily:

    var allChildControls = control.Controls.OfType<Control>().SelectMany(c => c.Controls.OfType<Control>());
    
    Rune Grimstad : As a side note. I needed to filter the Controls collection using OfType. I thought this wasn't necessary since the collection only returned Control objects. Does anybody know why this is needed?

0 comments:

Post a Comment

Note: Only a member of this blog may post a comment.