vbnewbie
vbnewbie

Reputation: 226

How to use for each in multiple control?

Im trying to clear all the field in textbox and uncheck all checkbox when the form on load

All my checkbox are inside TableLayoutPanel, but i have alot of TableLayoutPanel

Currently im using this method, but there is too many duplicate code

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

is that any other way to uncheck all the checkbox in diffrent TableLayoutPanel?

Edited:

Finally i get it done, but i dont think this is a good idea if the form has so many layer.

        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

Upvotes: 0

Views: 969

Answers (2)

Vivek S.
Vivek S.

Reputation: 21905

Loop through all TableLayoutPanel in your form and loop through textboxes,checkboxes in each TableLayoutPanel

   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

Upvotes: 2

Shadow Fiend
Shadow Fiend

Reputation: 351

try this

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

Upvotes: 0

Related Questions