Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Adding checkbox when checked?

Status
Not open for further replies.

rahulpatel

Programmer
Jan 20, 2007
33
AU
I have 10 checkboxes on a form. I want to show how many checkboxes are checked on the form.(label or textbox etc)

What is the best way to show how many are checked?

How do I add them up?

Cheers...
 
You can do something like:

Code:
dim i as integer=0

if checkbox1.checked then i+=1
if checkbox2.checked then i+=1
  .
  .
  .
if checkbox10.checked then i+=1

textbox1.text=i.tostring() & " are checked"

There is a better way ofcource, looping in the collection... but this may fit you.
 
If all of your Checkboxes are directly added to the form it would be as simple as this.
Code:
    Private Sub CountCheckedCheckboxes()
        Dim iCbxChecked As Integer
        For Each ctl As Control In Me.Controls
            If TypeOf (ctl) Is CheckBox Then
                If CType(ctl, CheckBox).Checked Then iCbxChecked += 1
            End If
        Next
        Label1.Text = String.Format("{0} Checkbox{1} checked.", iCbxChecked, _
            IIf(iCbxChecked = 1, "", "es"))
    End Sub
However, if your Checkbox controls are in other controls, GroupBoxes, Panels, Statusbars, etc... then you could do recursing.
Code:
    Private Sub CountCheckedCheckboxes(ByVal parentCtl As Control, ByVal resetCount As Boolean)
        Static iCbxChecked As Integer
        If resetCount Then iCbxChecked = 0
        For Each ctl As Control In parentCtl.Controls
            If ctl.Controls.Count > 0 Then
                CountCheckedCheckboxes(ctl, False)
            Else
                If TypeOf (ctl) Is CheckBox Then
                    If CType(ctl, CheckBox).Checked Then iCbxChecked += 1
                End If
            End If
        Next
        Label1.Text = String.Format("{0} Checkbox{1} checked.", iCbxChecked, _
            IIf(iCbxChecked = 1, "", "es"))
    End Sub
You will then use the following command to call the above code.
Code:
CountCheckedCheckBoxes(Me, True)

 
Alternatively, if you already have some code that runs on the CheckChanged event of each CheckBox, you could use that event handler to update a variable as:

[tt]
Private CheckCount As Integer = 0
...
...
-> CheckBox CheckChanged event handler
.. your current code
If <ThisCheckBox>.Checked Then CheckCount += 1 Else CheckCount -= 1
CheckCountLabel.Text = CheckCount.ToString + " CheckBoxes are checked"
[/tt]


<ThisCheckBox>.Checked can either be the name of the CheckBox that this handler is for, or the more generic
[tt]CType(Sender, CheckBox).Checked[/tt]

Hope this helps.

[vampire][bat]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top