This is a tutorial on how to clear all TextBoxes on a form in Visual Basic .NET.
Instead of calling each TextBox by its name and clear them one by one, you can use a For Each loop that clears all the TextBoxes found on the form

Here is the code that clears all TextBoxes text on a form
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim a As Control
        For Each a In Me.Controls
            If TypeOf a Is TextBox Then
                a.Text = Nothing
            End If
        Next
    End Sub
You can also specify textboxes in a groupbox to be cleared. In the following example we will clear all textboxes in GroupBox1 only

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
 
        Dim a As Control
 
        For Each a In Me.GroupBox1.Controls
 
            If TypeOf a Is TextBox Then
 
                a.Text = Nothing
 
            End If
 
        Next
 
    End Sub
No comments:
Post a Comment