How do I check and display if multiple checkboxes are checked within a group box in vb.net

390 views Asked by At
    For Each ctl As Windows.Forms.Control In Me.Controls
        If TypeOf ctl Is System.Windows.Forms.CheckBox Then
            Dim ck As System.Windows.Forms.CheckBox = ctl
            If ck.Checked Then
                intcheckboxesChecked += 1
            End If
        End If
    Next

When I display the count its results to a 0 value

1

There are 1 answers

0
user18387401 On BEST ANSWER

If the CheckBoxes are in a GroupBox then you have to use the Controls collection of the GroupBox rather than the form. Also, you can use a LINQ query to flatten your loop into a single statement:

Dim checkedBoxesCount = myGroupBox.Controls.
                                   OfType(Of CheckBox)().
                                   Count(Function(cb) cb.Checked)