Empty text field controls after data is inserted / saved / presented in winform c # application

I need to clear all textbox controls after clicking the SAVE button, but the user. I have about 10 of them. How to clear the text from them all at the same time. I just know about:

textbox1.Text="";

But, if I do this, then I need to repeat this for no. text box controls in my form, will it be a labor task instead of a programmer?

Please guide.

+5
source share
4 answers

try it

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

If you want to clear all text fields recursively, 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);
    }
}
+8
source

, :

foreach (var conrol in Controls)
{
    var textbox = conrol as TextBox;
    if (textbox != null)
        textbox.Clear();
}

, panel.Controls.

+2

API Linq, :

http://www.codeproject.com/KB/linq/LinqToTree.aspx#linqforms

Linq-to-XML Windows Forms. TextBox, 'this':

foreach(TextBox textbox in this.Descendants<TextBox>()
                               .Cast<TextBox>())
{
   textbox.Text = string.Empty;
}
+2

, , :

public static void ClearAllControls(Control.ControlCollection controls)
{
    foreach (var control in controls)
        ClearAllControls(control);
}

public static void ClearAllControls(Control control)
{           
    var textBox = control as TextBox
    if (textBox != null)
    {
        textBox.Text = null;
        return;
    }

    var comboBox = control as ComboBox;
    if (comboBox != null)
    {
        comboBox.SelectedIndex = -1;
        return;
    }

    // ...repeat blocks for other control types as needed

    ClearAllControls(control.Controls);
}

, Forms, , .., , . , . , , , .

, , , , "", , TextBoxes, , .

+2

All Articles