若要递归获取所有指定类型的控件和子控件,请使用以下扩展方法:
public static IEnumerable<TControl> GetChildControls<TControl>(this Control control) where TControl : Control{ var children = (control.Controls != null) ? control.Controls.OfType<TControl>() : Enumerable.Empty<TControl>(); return children.SelectMany(c => GetChildControls<TControl>(c)).Concat(children);}用法:
var allTextBoxes = this.GetChildControls<TextBox>();foreach (TextBox tb in allTextBoxes){ tb.Text = ...;}


