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

Checkboxes...I need a push in the right direction.

P: 1
Hello everyone I am new to learning coding in general and am looking for something that will help me with check boxes. I am working with the VB coding language as presented in Visual Studio 2010 Ultimate.

I want to know if it is possible to create certain events if a certain amount of check boxes are checked? There are 10 check boxes I want to do this with.

Basically this is what I am trying to do...

If ONLY ONE check box is checked, REGARDLESS of which one it is, I want a message to pop up.

If two check boxes are checked REGARDLESS of which ones they are a different message pops up BUT it ignores completing the first action if only one check box were checked.

Essentially I am trying to avoid the situation presented in this video where checking all of the boxes will produce multiple check boxes. I only want one message to pop up regardless of how many check boxes are checked, but I want a different message based on how many check boxes are checked.

I know the following isn't the coding required, but it may explain things a little better than I could?

Expand|Select|Wrap|Line Numbers
  4. EVENT A is this happening
  5. EVENT B is this happening
  6. EVENT C is this happening
  7. SOLUTION A is this
  8. SOLUTION B is this
  9. SOLUTION C is this
  11. IF "event a" THEN "solution a"  
  12. END IF
  14. IF "event b" THEN "solution b"
  15.     ELSE
  16.        ignore "event a" and "solution a" 
  17. END IF
  19. IF "event c" THEN "solution c" 
  20.     ELSE
  21.        ignore "event a" and "solution a" 
  22.     ELSE
  23.        ignore "event b" and "solution b" 
  24. END IF
  26. So I'm thinking some kind of counter is needed?
Jun 26 '14 #1
Share this Question
Share on Google+
1 Reply

P: 299
You could do something like what I've posted below. Hope it helps. All the checkBox events use the same action and the checked boxes are counted. Pretty straight forward:
Expand|Select|Wrap|Line Numbers
  1.     Private Sub CheckBoxAction()
  2.         Dim checkCount As Integer = 0
  4.         For Each checkedBox As CheckBox In Controls
  5.             If checkedBox.Checked = True Then
  6.                 checkCount = checkCount + 1
  7.             End If
  8.         Next
  10.         If checkCount = 1 Then
  11.             MsgBox("1")
  12.         ElseIf checkCount = 2 Then
  13.             MsgBox("2")
  14.         ElseIf checkCount = 3 Then
  15.             MsgBox("3")
  16.         ElseIf checkCount = 4 Then
  17.             MsgBox("4")
  18.         ElseIf checkCount = 5 Then
  19.             MsgBox("5")
  20.         End If
  21.     End Sub
  23.     Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged
  24.         CheckBoxAction()
  25.     End Sub
  27.     Private Sub CheckBox2_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox2.CheckedChanged
  28.         CheckBoxAction()
  29.     End Sub
  31.     Private Sub CheckBox3_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox3.CheckedChanged
  32.         CheckBoxAction()
  33.     End Sub
  35.     Private Sub CheckBox4_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox4.CheckedChanged
  36.         CheckBoxAction()
  37.     End Sub
  39.     Private Sub CheckBox5_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox5.CheckedChanged
  40.         CheckBoxAction()
  41.     End Sub
Jun 29 '14 #2

Post your reply

Sign in to post your reply or Sign up for a free account.