I got a blank project, i added 3 checkboxes, and 1 inside a group box.

And i got this code:

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        For Each phCheck As Control In Me.Controls
            If TypeOf phCheck Is CheckBox AndAlso DirectCast(phCheck, CheckBox).Checked Then
                If phCheck.Tag Is "Test" Then
                    DirectCast(phCheck, CheckBox).Checked = CBool(CheckState.Unchecked)
                End If
            End If
        Next
    End Sub

I added the tag "Test" to all the checkboxes, and when i click the button all checkboxes goes to unchecked except the one inside the group box, how can i loop through the controls inside of a container ?

Recommended Answers

All 4 Replies

Instead of doing 'in Me.Controls', you have to do 'in Me.GroupBox1.Controls'.
But since your testing controls out and in the groupbox, you gotta check both.

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        For Each phCheck As Control In Me.Controls
            If TypeOf phCheck Is CheckBox AndAlso DirectCast(phCheck, CheckBox).Checked Then
                If phCheck.Tag Is "Test" Then
                    DirectCast(phCheck, CheckBox).Checked = False
                End If
            End If
        Next
        For Each phCheck As Control In Me.GroupBox1.Controls
            If TypeOf phCheck Is CheckBox AndAlso DirectCast(phCheck, CheckBox).Checked Then
                If phCheck.Tag Is "Test" Then
                    DirectCast(phCheck, CheckBox).Checked = False
                End If
            End If
        Next
    End Sub

And 'CBool(CheckState.Unchecked)' and 'False' are the same thing.

commented: Thanks for trying, but not exactly what i wanted :P +2

You can simplify the loops by using

For Each phCheck As CheckBox In Me.Controls.OfType(Of CheckBox)()

That will iterate through only CheckBox controls.

commented: Thanks for trying :) it have me same result as my code :P +2

Not to beat a dead horse, but you could just call this Sub like the following:

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Call DoControls(Me)
    End Sub

    Private Sub DoControls(ByVal ctrlCont As Control)
        For Each ctrl As Control In ctrlCont.Controls
            If TypeOf ctrl Is CheckBox Then
                If TypeOf ctrl Is CheckBox AndAlso DirectCast(ctrl, CheckBox).Checked Then
                    If ctrl.Tag Is "Test" Then
                        DirectCast(ctrl, CheckBox).Checked = False
                    End If
                End If
            End If

            'If the ctrl has children, 
            'recall this Sub
            If ctrl.HasChildren Then
                DoControls(ctrl)
            End If
        Next
    End Sub
commented: Worked :) Thank you +2

@Pride: Yeah i know i can do that but i didn't want to add so many for each groupboxs & panels.

@Jim: Same result as my code, it will only do it in the form and not in other containers(Groupboxs,Panels, etc)

@kRod: A bit long, but worked. thx :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.