Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

empty textbox controls after the data is inserted / saved/ submitted in a c# winform application

I need to empty all the textbox controls after the SAVE button is clicked but the user. I have around 10 of them. How do i clear text from them all simultaneously. I just know about:

textbox1.Text="";

But, if i do this, then i need to repeat this for the no. of textbox controls on my Form, that would be a labor task instead of programmer?

Please guide.

like image 630
sqlchild Avatar asked May 17 '11 07:05

sqlchild


1 Answers

Try this

foreach(TextBox textbox in this.Controls.OfType<TextBox>())
{
   textbox.Text = string.Empty;
}

If you want recursivly clear all textboxes use this function.

void ClearTextBoxes(Control control)
{
    foreach(Control childControl in control.Controls)
    {
         TextBox textbox = childControl as TextBox;
         if(textbox != null)
            textbox.Text = string.Empty;
         else if(childControl.Controls.Count > 0)
             ClearTextBoxes(childControl);
    }
}
like image 169
Stecya Avatar answered Sep 24 '22 13:09

Stecya