By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,694 Members | 1,320 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,694 IT Pros & Developers. It's quick & easy.

Limiting checkboxes - only check 2 of 5 - help needed

P: n/a
Hello - I am designing a form that only allows 2 checkboxes to be checked
out of a series of checkboxes. However, checkedchanged and checkstatechanged
both act when the box is clicked on. I want only 2 boxes clicked, and if
another is selected nothing happens until one is unselected.

I tried keeping track of how many were checked then turning off all the
checkboxes, but that didn't seem right or allow changes. Also, if this could
be done easier with radiobuttons or something, hey, I'm all ears!
Nov 21 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
EC
The following assumes 4 checkboxes on the form, with only two
selections allowed at a time. It's not sexy, but it gets the job done.
Private Sub AllBoxes_CheckedChanged(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged,
CheckBox2.CheckedChanged, CheckBox3.CheckedChanged,
CheckBox4.CheckedChanged
' Count the # of checked boxes
Dim qty As Int16 = 0
Dim cb As CheckBox
For Each cb In Me.Controls
If cb.Checked Then
qty += 1
End If
Next
If qty = 2 Then
' Disable the rest
For Each cb In Me.Controls
If Not cb.Checked Then
cb.Enabled = False
End If
Next
Else
' Enable all
For Each cb In Me.Controls
cb.Enabled = True
Next
End If
End Sub

Patrick wrote:
Hello - I am designing a form that only allows 2 checkboxes to be checked out of a series of checkboxes. However, checkedchanged and checkstatechanged both act when the box is clicked on. I want only 2 boxes clicked, and if another is selected nothing happens until one is unselected.

I tried keeping track of how many were checked then turning off all the checkboxes, but that didn't seem right or allow changes. Also, if this could be done easier with radiobuttons or something, hey, I'm all ears!


Nov 21 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.