Clearing of Multiple Textboxes in just a Click Using Visual Basic 2008

If you are a beginner in Programming Languages and you’re using Visual Basic, this tutorial will help you minimize your code and lessen your work. This procedure is to clear all the data in the TextBoxes in a certain Form, for example, when you create a “User Entry” Form. So let’s begin: First let’s create a Form, then put all the TextBoxes you need. User Entry Double click the Form, then you have to create a Public Sub and it will look like this.
  1. 'create your Sub procedure named "clearall" with a parameters type as object.
  2. Public Sub clearall(ByVal form As Object)
  3. 'for each txt as control in form(object).control
  4. For Each txt As Control In form.Controls
  5. 'conditioning the txt as control by getting it's type.
  6. 'the type of txt as control must be textbox.
  7. If txt.GetType Is GetType(TextBox) Then
  8. 'if the object(textbox) is present. The result is, the textbox will be cleared.
  9. txt.Text = Nothing
  10. End If
  11. Next
  12. End Sub
Description: By using this code, all data in the TextBoxes that can be seen in a Form will all be cleared. Go back to the Design Views, then double click the Clear Button and call the Sub Name in the Public Sub that you’ve created. It will look like this.
  1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  2. 'confirming that the data will be clear
  3. MsgBox("data cleared")
  4.  
  5. 'call your Sub name.
  6. clearall(Me)
  7.  
  8. End Sub
“Me” is an object, which stands as a Form. Now run your project. You can also download the Source Code of this project and run it on your computer.

Add new comment