Clear TextBox in C#

  1. Clear a TextBox With the String.Empty Property in C
  2. Clear a TextBox With the TextBox.Text="" Method in C
  3. Clear a TextBox With the TextBox.Clear() Function in C

This tutorial will discuss the methods to clear a text box in C#.

Clear a TextBox With the String.Empty Property in C

The String.Empty property represents an empty string in C#. It is equivalent to "" in C#. We can set the text of our TextBox to be equal to String.Empty to clear our text box. The following code example shows us how we can clear a text box with the String.Empty property in C#.

using System;
using System.Windows.Forms;

namespace check_if_textbox_is_empty
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            textBox1.Text = String.Empty;
        }
    }
}

In the above code, we cleared all the text inside the textBox1 text box by making it equal to String.Empty in C#.

Clear a TextBox With the TextBox.Text="" Method in C

In the previous approach, the String.Empty property represented an empty string and was equal to "". We can also clear our text box by making the TextBox.Text property equal to "". The following code example shows us how we can clear a text box with the TextBox.Text="" method in C#.

using System;
using System.Windows.Forms;

namespace check_if_textbox_is_empty
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            textBox1.Text = "";
        }
    }
}

In the above code, we cleared all the text inside the textBox1 text box, by making it equal to "" in C#.

Clear a TextBox With the TextBox.Clear() Function in C

The TextBox.Clear() function is used to clear all the text inside a text box in C#. The following code example shows us how we can clear a text box with the TextBox.Clear() function in C#.

using System;
using System.Windows.Forms;

namespace check_if_textbox_is_empty
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            textBox1.Clear();
        }
    }
}

In the above code, we cleared all the text inside the textBox1 text box with the textBox1.Clear() function in C#.

Contribute
DelftStack is a collective effort contributed by software geeks like you. If you like the article and would like to contribute to DelftStack by writing paid articles, you can check the write for us page.

Related Article - Csharp GUI

  • TextBox New Line in C#
  • Clear ListBox in C#