0
    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

Jimi
  • 29,621
  • 8
  • 43
  • 61
Ahmed
  • 3
  • 1
  • `For Each ctl As CheckBox In [GroupBox].Controls.OfType(Of CheckBox) If ctl.Checked Then ...` -- You need to loop the Controls collection of the actual Parent -- This is VB.Net, VB6 is unrelated. I removed the tag. – Jimi Apr 16 '22 at 08:52
  • As general advice, you should turn `Option Strict On` in the project properties and in the VS options, so it will be `On` by default in future projects. You obviously have it set `Off` as your third line wouldn't compile otherwise. Setting it `On` will force you to think about data types and write more robust code. – user18387401 Apr 16 '22 at 09:07

1 Answers1

1

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)
user18387401
  • 2,514
  • 1
  • 3
  • 8