Is it possible to clear containts of all textboxes on a form by issusing one command TIA

  • Thread starter Thread starter Guest
  • Start date Start date
You can create this functionality yourself by running through the Controls
collection of the Form:

foreach (Control ctrl in this.Controls)
{
if (ctrl is TextBox)
{
((TextBox)ctrl).Clear();
}
}
 
* "Tim Wilson said:
You can create this functionality yourself by running through the Controls
collection of the Form:

foreach (Control ctrl in this.Controls)
{
if (ctrl is TextBox)
{
((TextBox)ctrl).Clear();
}
}

Note: This won't clear nested textboxes...
 
True. Recursion is the way to go to clear every textbox directly or
indirectly on the form. This was just a simple piece of code I threw
together to demo the basic understanding of how to locate and clear
textbox's dynamically. Thanks for pointing this out as I should have in my
original post :)
 
* "Tim Wilson said:
True. Recursion is the way to go to clear every textbox directly or
indirectly on the form. This was just a simple piece of code I threw
together to demo the basic understanding of how to locate and clear
textbox's dynamically. Thanks for pointing this out as I should have in my
original post :)

No problem -- it was only a little note -- maybe a "TODO" for the OP...

;-)
 
Ok, so I broke down and wrote the recursive code:

private void button1_Click(object sender, System.EventArgs e)
{
ClearAllTextBoxes(this);
}

private void ClearAllTextBoxes(Control parent)
{
if (parent is TextBox)
{
((TextBox)parent).Clear();
}
foreach (Control ctrl in parent.Controls)
{
ClearAllTextBoxes(ctrl);
}
}

.... I am easily susceptible to peer pressure :)
 
Back
Top