如何在多重控制中使用每个?

时间:2016-12-01 09:25:18

标签: vb.net loops checkbox foreach

我试图清除文本框中的所有字段,并在加载表单时取消选中所有复选框

我的所有复选框都在TableLayoutPanel中,但我有很多TableLayoutPanel

目前我正在使用此方法,但有太多重复的代码

Private Sub ResetPage()      

    Dim ctrl As Control

    For Each ctrl In tlp_userInfo.Controls
        If TypeOf ctrl Is TextBox Then
            ctrl.Text = Nothing
        End If
        If TypeOf ctrl Is ComboBox Then
            ctrl.Text = Nothing
        End If
    Next

    For Each ctrl In tlp_chkb1.Controls
        If TypeOf ctrl Is CheckBox Then
            DirectCast(ctrl, CheckBox).Checked = False
        End If
    Next

    For Each ctrl In tlp_chkb2.Controls
        If TypeOf ctrl Is CheckBox Then
            DirectCast(ctrl, CheckBox).Checked = False
        End If
    Next

    For Each ctrl In tlp_chkb3.Controls
        If TypeOf ctrl Is CheckBox Then
            DirectCast(ctrl, CheckBox).Checked = False
        End If
    Next

End Sub

是否可以取消选中不同的TableLayoutPanel中的所有复选框?

编辑:

最后我完成了它,但我不认为如果表单有这么多层,这是一个好主意。

        For Each ctrl_layer1 As Control In Me.Controls
        If TypeOf ctrl_layer1 Is TableLayoutPanel Then
            For Each ctrl_layer2 As Control In ctrl_layer1.Controls
                If TypeOf ctrl_layer2 Is TableLayoutPanel Then
                    For Each ctrl_layer3 As Control In ctrl_layer2.Controls
                        If TypeOf ctrl_layer3 Is TableLayoutPanel Then
                            For Each ctrl_layer4 In ctrl_layer3.Controls
                                If TypeOf ctrl_layer4 Is TextBox Then
                                    ctrl_layer4.Text = ""
                                ElseIf TypeOf ctrl_layer4 Is ComboBox Then
                                    ctrl_layer4.Text = ""
                                ElseIf TypeOf ctrl_layer4 Is TabControl Then
                                    For Each ctrl_layer5 As Control In ctrl_layer4.controls
                                        If TypeOf ctrl_layer5 Is TabPage Then
                                            For Each ctrl_layer6 In ctrl_layer5.Controls
                                                If TypeOf ctrl_layer6 Is TableLayoutPanel Then
                                                    For Each ctrl_layer7 In ctrl_layer6.controls
                                                        If TypeOf ctrl_layer7 Is TableLayoutPanel Then
                                                            For Each ctrl_layer8 In ctrl_layer7.controls
                                                                If TypeOf ctrl_layer8 Is CheckBox Then
                                                                    DirectCast(ctrl_layer8, CheckBox).Checked = False
                                                                End If
                                                            Next
                                                        End If
                                                    Next
                                                End If
                                            Next
                                        End If
                                    Next
                                End If
                            Next
                        End If
                    Next
                End If
            Next
        End If
    Next

2 个答案:

答案 0 :(得分:1)

遍历表单中的所有TableLayoutPanel,然后在每个textboxes

中循环浏览checkboxesTableLayoutPanel
   For Each ctrl_tlo As Control In Me.Controls
      If TypeOf (ctrl_tlo) Is TableLayoutPanel Then
         For Each ctrl As Control In ctrl_tlo.Controls
             If TypeOf (ctrl) Is TextBox Then
                 ctrl.Text = ""
             ElseIf TypeOf (ctrl) Is CheckBox Then
                 DirectCast(ctrl, CheckBox).Checked = False
             End If
         Next
      End If
   Next

答案 1 :(得分:0)

试试这个

Dim SetofPanels = {Name of your panels with comma between each controls}
For Each ctrl In SetofPanels 

If TypeOf ctrl Is TextBox Then
       ctrl.Text = Nothing
End If

If TypeOf ctrl Is ComboBox Then
       ctrl.Text = Nothing
End If

If TypeOf ctrl Is CheckBox Then
       DirectCast(ctrl, CheckBox).Checked = False
End If

Next