I need to loop through absolutely all controls in form (including their children) in the cycle. With help this.controls the current children are exactly the forms, and I need that in the array were all controls in the form, how to do it?

    1 answer 1

    Use recursion.
    Perform an action on each control:

    public static void ForAllControls(this Control parent, Action<Control> action) { foreach (Control c in parent.Controls) { action(c); ForAllControls(c, action); } } 

    Get all controls of the specified type:

     public static IEnumerable<Control> GetAllControls(this Control control, Type type) { var controls = control.Controls.Cast<Control>().ToArray(); return controls.SelectMany(ctrl => GetAllControls(ctrl, type)) .Concat(controls) .Where(c => c.GetType() == type); } 
    • Thanks, the first option used, it worked - Minebot