Mike
Mike

Reputation: 325

How to clear all data / string in a TextBox in C#

I have had a quick look on Google and didn't pull anything simple up.

I have tried:

textBox5.Clear();

textBox5.Text=("");

textBox5.Text="".ToString();

Upvotes: 0

Views: 26331

Answers (3)

Umair Rasheed
Umair Rasheed

Reputation: 448

1st Make method

public void cleardata(Control.ControlCollection cc)
    {
        foreach (Control ctrl in cc)
        {
            //for textbox
            TextBox tb = ctrl as TextBox;
            if (tb!=null)
            {
                tb.Text = "";
            }
            //for combobox
            ComboBox cb = ctrl as ComboBox;
            if (cb!=null)
            {
                cb.ResetText();
                cb.Text = null;
            }
            //for rich textbox
            RichTextBox rtxtbox = ctrl as RichTextBox;
            if (rtxtbox!=null)
            {
                rtxtbox.Clear();
            }
            //for radio button
            if (ctrl is RadioButton)
            {
                RadioButton rb = ctrl as RadioButton;
                if (rb.Checked==true)
                {
                    rb.Checked = false;
                }
            }
            //for check box
            if (ctrl is CheckBox)
            {
                CheckBox _cb = ctrl as CheckBox;
                if (_cb.Checked==true)
                {
                    _cb.Checked = false;
                }
            }
            //for date time picker
            if (ctrl is DateTimePicker)
            {
                DateTimePicker dt = ctrl as DateTimePicker;
                if (dt.Value!=DateTime.Now)
                {
                    dt.Value = DateTime.Now;
                }
            }
            //for Picture box
            if (ctrl is PictureBox)
            {
                PictureBox Pic_box = ctrl as PictureBox;
                if (Pic_box.Image!=Pic_box.InitialImage)
                {
                    Pic_box.Image = Pic_box.InitialImage;
                }
            }
            //for numeric up down
            if (ctrl is NumericUpDown)
            {
                NumericUpDown num_up_down = ctrl as NumericUpDown;
                if (num_up_down.Value!=0)
                {
                    num_up_down.Value = 0;
                }
            }
            
            else
            {
                cleardata(ctrl.Controls);
            }
        }
    }

Then call method

cleardata(this.Controls);

Video is here

Upvotes: 1

Oscar Gomez
Oscar Gomez

Reputation: 18488

You could try:

 foreach ( Control ctl in Parent.Controls) {
 if (ctl.GetTyep()== TextBox)
        ctl.text = ""; 
 } 

Also why are you doing

    textbox.Text = (""); //You don't need the parenthesis here
    textbox.Text = "".toString(); //the .toString() is definitly not needed.

If you only want it for a single textbox then:

myTextBox.Text = String.Empty; 

or

 myTextBox.Text = "";

Upvotes: 4

Kinexus
Kinexus

Reputation: 12904

TextBox5.Text = string.Empty;

Should not be any more difficult than that.

Upvotes: 1

Related Questions