Трюк здесь в том, что Controls
не является List<>
или IEnumerable
, а ControlCollection
.
Я рекомендую использовать расширение Control, которое вернет что-то большее. )
public static IEnumerable<Control> All(this ControlCollection controls)
{
foreach (Control control in controls)
{
foreach (Control grandChild in control.Controls.All())
yield return grandChild;
yield return control;
}
}
Тогда вы можете сделать:
foreach(var textbox in this.Controls.All().OfType<TextBox>)
{
// Apply logic to the textbox here
}