Visual Basic - Use The TypeOf Function To Quickly Clear All Controls From A Form (VB 6)


By using the following code, you can quickly clear all the text boxes on your form:


  • Dim vControl as variant
  •  
  • For Each vControl In Me.Controls
  •   If TypeOf vControl Is TextBox Then vControl.Text = ""
  • Next

This code also works in VB .NET, but now you can take advantage of the TextBox Clear property, like so:


  • Dim vControl as Object
  •  
  • For Each vControl In Me.Controls
  •    If TypeOf vControl Is TextBox Then vControl.Clear()
  • Next

Go back