0

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

Edited by zarifin99ska: n/a

3
Contributors
3
Replies
9
Views
5 Years
Discussion Span
Last Post by codeorder
0

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
0

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

i have try many ways and it still says 0.

Edited by zarifin99ska: n/a

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?

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.