Smart Way to Clear Multiple TextBoxes in C#

In this tutorial, I will teach you how to clear multiple textboxes in c#. This method has the ability to clear all the data inside the textbox in just a click. This is a big help for you when you have multiple textboxes inside the form. Let’s begin.

Step 1

Open Microsoft Visual Studio 2015 and create a new windows form application in c#. ps1

Step 2

Do the form just like shown below. ps2

Step 3

Create a method for clearing all data inside the textboxes in the form.
  1.         private void clear_all()
  2.         {
  3.             try
  4.             {
  5.                 foreach(Control txt in this.Controls)
  6.                 {
  7.                    
  8.                     if(txt is TextBox)
  9.                     {
  10.                        txt.Text = "";
  11.                     }
  12.                 }
  13.             }
  14.             catch (Exception ex)
  15.             {
  16.                 MessageBox.Show(ex.Message);
  17.             }
  18.         }

Step 4

Do the following code to clear all data when the button is clicked.
  1.         private void button2_Click(object sender, EventArgs e)
  2.         {
  3.             clear_all();
  4.         }
Download the complete sourcecode and run it on your computer. For any questions about this article. You can contact me @ Email – [email protected] Mobile No. – 09305235027 – TNT Or feel free to comment below

Add new comment