How can I use a Foreach Statement to do something to my TextBoxes?
foreach (Control X in this.Controls)
{
Check if the controls is a TextBox, if it is de
The trick here is that Controls
is not a List<>
or IEnumerable
but a ControlCollection
.
I recommend using an extension of Control that will return something more..queriyable ;)
public static IEnumerable All(this ControlCollection controls)
{
foreach (Control control in controls)
{
foreach (Control grandChild in control.Controls.All())
yield return grandChild;
yield return control;
}
}
Then you can do :
foreach(var textbox in this.Controls.All().OfType)
{
// Apply logic to the textbox here
}