清除面板内

时间:2017-06-23 15:20:47

标签: vb.net winforms

    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

我有这种方法,但问题是,我的组框出现在一个面板中,只是被这些控件混淆了很多。

3 个答案:

答案 0 :(得分:3)

最简单的方法是只过滤枚举中的控件: 对于每个p作为面板在Frm.Controls.OfType(面板)()   对于每个gb作为GroupBox在p.Controls.OfType(Of GroupBox)()     For each tb As TextBox in gb.Controls.OfType(Of TextBox)()       tb.Clear()     下一个   下一个 下一个

答案 1 :(得分:1)

这是一种方法:

   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

它可能无法涵盖所有​​情况,但它适用于您告诉我们的内容。

答案 2 :(得分:0)

感谢您的所有建议,我得出结论,此代码对我来说非常适合。

    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