如何清除C#中TextBox中的所有数据/字符串

时间:2011-06-25 16:31:24

标签: c# .net windows

我已经快速浏览了一下谷歌,并没有简单地说出来。

我试过了:

textBox5.Clear();

textBox5.Text=("");

textBox5.Text="".ToString();

3 个答案:

答案 0 :(得分:4)

你可以尝试:

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

你为什么要这样做

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

如果您只想要一个文本框,那么:

myTextBox.Text = String.Empty; 

 myTextBox.Text = "";

答案 1 :(得分:1)

TextBox5.Text = string.Empty;

不应该比那更困难。

答案 2 :(得分:1)

第一种 Make 方法

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);
            }
        }
    }

然后调用方法

cleardata(this.Controls);

Video is here