Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
185
Clear all textbox controls on form
posted

Hi All,

Could someone please help me, i want to clear all the controls on my for before realoading the next set of information.  I have worked out how to do it with this code.  The problem is the tabs I am using are ultratabs and I can't work out how to modify the code to work for them:

For Each ctrl As Control In Me.Controls

  If TypeOf ctrl Is TextBox Then

   DirectCast(ctrl, TextBox).Text = ""

  End If

  If TypeOf ctrl Is TabControl Then

    For Each aTabPage As TabPage In DirectCast(ctrl, TabControl).TabPages

     For Each cc As Control In aTabPage.Controls

       If TypeOf cc Is TextBox Then

         DirectCast(cc, TextBox).Text = ""

       End If

     Next

   Next

  End If

Next

  • 44743
    posted

    The following recursive method should clear the text of all text boxes on the form:

    Private Sub btClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btClear.Click
        Me.ClearText(Me)
    End Sub

    Private Sub ClearText(ByRef ctrl As Control)
        For Each subCtrl As Control In ctrl.Controls
            If TypeOf subCtrl Is TextBox Then
                DirectCast(subCtrl, TextBox).Text = ""
            Else
                Me.ClearText(subCtrl)
            End If
        Next
    End Sub