none
Best practice to clear Textbox contents with button click

    Question

  • Hi all,

    I have 10 textboxs and a button on my page. The button clear is programed to clear the contents of the text boxes. Here is the code:

            private void clear_icrs_button_Click(object sender, EventArgs e)
            {
                textBox1.Text = "";
                textBox2.Text = "";
                textBox3.Text = "";
                textBox4.Text = "";
                textBox5.Text = "";
                textBox6.Text = "";
                textBox7.Text = "";
                textBox8.Text = "";
                textBox9.Text = "";
                textBox10.Text = "";
            }

    Is there a better way to write this? What if I had 50 textboxs or 150 that made up my form?

    Thanks.


    Lee Warren

    Friday, November 23, 2012 2:10 PM

Answers

  • For  clearing all text boxes on a form you can use something like this.

    private void clear_icrs_button_Click(object sender, EventArgs e)
    {
    	this.TraverseControlsAndSetTextEmpty(this);
    }
    
    private void TraverseControlsAndSetTextEmpty(Control control)
    {
    	foreach (Control c in control.Controls)
    	{
    		var box = c as TextBox;
    		if (box != null)
    		{
    			box.Text = string.Empty;
    		}
    
    		this.TraverseControlsAndSetTextEmpty(c);
    	}
    }

    Modified from this SO question


    Hope this helps,

    Here to learn and share. Please tell if an answer was helpful or not at all. This adds value to the answers and enables me to learn more.

    About me

    Friday, November 23, 2012 2:30 PM

All replies

  • For  clearing all text boxes on a form you can use something like this.

    private void clear_icrs_button_Click(object sender, EventArgs e)
    {
    	this.TraverseControlsAndSetTextEmpty(this);
    }
    
    private void TraverseControlsAndSetTextEmpty(Control control)
    {
    	foreach (Control c in control.Controls)
    	{
    		var box = c as TextBox;
    		if (box != null)
    		{
    			box.Text = string.Empty;
    		}
    
    		this.TraverseControlsAndSetTextEmpty(c);
    	}
    }

    Modified from this SO question


    Hope this helps,

    Here to learn and share. Please tell if an answer was helpful or not at all. This adds value to the answers and enables me to learn more.

    About me

    Friday, November 23, 2012 2:30 PM
  • Many thanks for your quick reply. That's perfect!

    Thanks.


    Lee Warren

    Friday, November 23, 2012 2:43 PM
  • I tried this as well, however, I am getting an error on the  .Controls:

    Error 1 'System.Windows.Controls.Control' does not contain a definition for 'Controls' and no extension method 'Controls' accepting a first argument of type 'System.Windows.Controls.Control' could be found (are you missing a using directive or an assembly reference?) 

    Any suggestions?

    Friday, April 26, 2013 3:23 PM
  • I tried this as well, however, I am getting an error on the  .Controls:

    Error 1 'System.Windows.Controls.Control' does not contain a definition for 'Controls' and no extension method 'Controls' accepting a first argument of type 'System.Windows.Controls.Control' could be found (are you missing a using directive or an assembly reference?) 

    Any suggestions?

    You are using WPF and this example is for Windows Forms?

    Mark the best replies as answers. "Fooling computers since 1971."

    http://thesharpercoder.com/

    Friday, April 26, 2013 6:17 PM
    Moderator