How to Clear Multiple Textbox in a Smart Way using Visual Basic

In this tutorial, I'm going to show how to create an application that will help you to save more time and number of lines of code just to clear your multiple textbox in a single form. This time, let's start building our application: Step 1. Create a new project in visual Basic 2008, then design the interface that looks like as shown below: c1
Step 2. Double click the “Using "" ” button. Then add the following code:
  1. TextBox1.Text = ""
  2. TextBox2.Text = ""
  3. TextBox3.Text = ""
  4. TextBox4.Text = ""
  5. TextBox5.Text = ""
  6.  
Explanation: The added code code in step 2, we simply set the text value of a text to an empty string. Step 3. Double click the “Using Nothing” button and add the following code:
  1.         TextBox1.Text = Nothing
  2.         TextBox2.Text = Nothing
  3.         TextBox3.Text = Nothing
  4.         TextBox4.Text = Nothing
  5.         TextBox5.Text = Nothing
Explanation: The new added code in step 3, we just set the text value of a textbox to Nothing. Step 4. Double click the “Using Smart Clear” button and add the following code:
  1. For Each ctrl As Control In Controls
  2.             If ctrl.GetType Is GetType(TextBox) Then
  3.                 ctrl.Text = Nothing
  4.             End If
  5.         Next
Explanation: In the new added code in step 4, we use “For Each Next loop”. We set a variable “ctrl” as Control in “Controls” to get the collection of control contained within the control. Then we use the conditional logic, if statement to check if the Type of control is Textbox, then it will set the Text property of a textbox into “Nothing”.

Add new comment