Public Sub ClearTextBoxes(ByVal Frm As Form)
     Dim Ctl As Control
     For Each Ctl In Frm.Controls
        If TypeOf Ctl Is TextBox Then Ctl.Text = ""
        If TypeOf Ctl Is GroupBox Then
            Dim Ctl1 As Control
            For Each Ctl1 In Ctl.Controls
                If TypeOf Ctl1 Is TextBox Then
                    Ctl1.Text = ""
                End If
            Next
        End If
        Next
      End Sub

Tengo este método, pero el problema es que mis cuadros de grupo están presentes dentro de un panel, simplemente confundidos por estos controles.

0
Wes Gourh 23 jun. 2017 a las 18:20

3 respuestas

La mejor respuesta

Aquí hay una forma de hacerlo:

   Public Sub ClearTextBoxes(ByVal Frm As Form)
      Dim Ctl As Control

      For Each Ctl In Frm.Controls
         If TypeOf Ctl Is TextBox Then Ctl.Text = ""

         If TypeOf Ctl Is Panel Then
            Dim Ctl1 As Control

            For Each Ctl1 In Ctl.Controls
               If TypeOf Ctl1 Is TextBox Then Ctl1.Text = ""

               If TypeOf Ctl1 Is GroupBox Then
                  Dim Ctl2 As Control

                  For Each Ctl2 In Ctl1.Controls
                     If TypeOf Ctl2 Is TextBox Then Ctl2.Text = ""
                  Next
               End If
            Next
         End If
      Next
   End Sub

Puede que no cubra todos los escenarios, pero funcionará para lo que nos ha dicho.

1
Brian M Stafford 23 jun. 2017 a las 15:31

Gracias por todas sus sugerencias, he concluido que este código funciona perfecto para mí.

    Public Sub ClearTextBoxes(ByVal Frm As Form)
    Dim Ctl As Control

    For Each Ctl In Frm.Controls
        If TypeOf Ctl Is TextBox Then Ctl.Text = ""

        If TypeOf Ctl Is Panel Then
            Dim Ctl1 As Control

            For Each Ctl1 In Ctl.Controls
                If TypeOf Ctl1 Is TextBox Then Ctl1.Text = ""

                If TypeOf Ctl1 Is GroupBox Then
                    Dim Ctl2 As Control

                    For Each Ctl2 In Ctl1.Controls
                        If TypeOf Ctl2 Is TextBox Then Ctl2.Text = ""
                    Next
                End If
            Next
        End If

        If TypeOf Ctl Is GroupBox Then
            Dim Ctl1 As Control
            For Each Ctl1 In Ctl.Controls
                If TypeOf Ctl1 Is TextBox Then
                    Ctl1.Text = ""
                End If
            Next
        End If
    Next
End Sub
0
Wes Gourh 25 jun. 2017 a las 08:06

La manera más simple es simplemente filtrar los controles en las enumeraciones:

For Each p As Panel In Frm.Controls.OfType(Of Panel)()
  For Each gb As GroupBox In p.Controls.OfType(Of GroupBox)()
    For Each tb As TextBox In gb.Controls.OfType(Of TextBox)()
      tb.Clear()
    Next
  Next
Next
3
LarsTech 23 jun. 2017 a las 15:33