Hi. I have a form contain 10 checkbox. how to count total checkboxes that has been checked? i search over the internet but not found any solution. please help me. i found a code, but when i apply it on my project it says "Unable to cast object of type System.Windows.Forms.MenuStrip' to type ComponentFactory.Krypton.Toolkit.KryptonCheckBox'". i think i have problems with Me.Controls(on the code)

this is the code, might helping you all.
note: i also use krypton toolkit (krypton checkbox).

Dim intTotalCheckBoxes As Integer = 0
    Dim intCheckBoxesChecked As Integer = 0



 For Each cb As checkbox In Me.Controls
            If cb.Checked = True Then
                intCheckBoxesChecked += 1
            End If
            intTotalCheckBoxes += 1

        Next
        lbl5.Text = intTotalCheckBoxes.ToString

I'll try to help you out here.

First off, you should not assume that every control in Me.Controls is a checkbox. Essentially, though, that is what you did when you wrote the "for each" statement.

Instead try this:

Dim intcheckboxesChecked As Integer = 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

then, how to put intcheckboxesChecked.tostring into a label or a textbox?

i have try many ways and it still says 0.

>>then, how to put intcheckboxesChecked.tostring into a label or a textbox?
Why not add it as you did in your original code, just following the For/Next loop?

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.