简体   繁体   中英

Clear multiple textboxes present inside a groupbox which is inside a panel

    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

I have this method, but the problem is, my groupboxes are present inside a panel, just confused a lot by these controls.

Simplest way is to just filter the controls in the enumerations:

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

Here is one way of doing it:

   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

It may not cover all scenarios but it will work for what you have told us.

Thank you for all your suggestions, I have concluded that this code works perfect for me.

    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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM