Troubling with checkboxes. Code for limiting Checkboxes Checked

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mykill007
    New Member
    • Jan 2014
    • 5

    Troubling with checkboxes. Code for limiting Checkboxes Checked

    [z{First Post from First Thread}]

    Hi guys. I'm looking for a code that would let me choose only 3 out of the 6 checkboxes. After I checked 3 checkboxes the other checkboxes would be Grayed.

    P.S
    Visual Basic 6.0

    [z{First Post from Second Thread}]
    Hi. I have 6 check boxes but I only want to check 3. Can u please mail me or reply me the exact codes I should Use. I tried this http://bytes.com/topic/visual-basic/...sable-checkbox
    But it didn't work on me. Please help and thank you
    Last edited by zmbd; Jan 23 '14, 11:27 PM. Reason: [z{Please do not double post, nor post in offtopic threads. In the future both might be deleted}]
  • Luk3r
    Contributor
    • Jan 2014
    • 300

    #2
    Can you show us the code that you are currently using that is not working so we can build upon it before someone writes code for you?

    Comment

    • mykill007
      New Member
      • Jan 2014
      • 5

      #3
      I tried this one but it didnt work for me
      Code:
      Private Sub CheckBox1_Change()
          If CheckBox1.Value = False Then
              Call Enable_All
          Else
              Call Disable_All
          End If
      End Sub
       
      Private Sub CheckBox2_Change()
          If CheckBox2.Value = False Then
              Call Enable_All
          Else
              Call Disable_All
          End If
      End Sub
       
      Private Sub CheckBox3_Change()
          If CheckBox3.Value = False Then
              Call Enable_All
          Else
              Call Disable_All
          End If
      End Sub
       
      Private Sub CheckBox4_Change()
          If CheckBox4.Value = False Then
              Call Enable_All
          Else
              Call Disable_All
          End If
      End Sub
       
       
      Private Sub CheckBox5_Change()
          If CheckBox5.Value = False Then
              Call Enable_All
          Else
              Call Disable_All
          End If
      End Sub
       
      Private Sub CheckBox6_Change()
          If CheckBox6.Value = False Then
              Call Enable_All
          Else
              Call Disable_All
          End If
      End Sub
       
      Private Sub CheckBox7_Change()
          If CheckBox7.Value = False Then
              Call Enable_All
          Else
              Call Disable_All
          End If
      End Sub
       
      Private Sub CheckBox8_Change()
          If CheckBox8.Value = False Then
              Call Enable_All
          Else
              Call Disable_All
          End If
      End Sub
       
      Private Sub CheckBox9_Change()
          If CheckBox9.Value = False Then
              Call Enable_All
          Else
              Call Disable_All
          End If
      End Sub
       
      Private Sub CheckBox10_Change()
          If CheckBox10.Value = False Then
              Call Enable_All
          Else
              Call Disable_All
          End If
      End Sub
      Private Sub Disable_All()
      Dim CHECKEDcount As Integer
          If CheckBox1.Value = True Then CHECKEDcount = CHECKEDcount + 1
          If CheckBox2.Value = True Then CHECKEDcount = CHECKEDcount + 1
          If CheckBox3.Value = True Then CHECKEDcount = CHECKEDcount + 1
          If CheckBox4.Value = True Then CHECKEDcount = CHECKEDcount + 1
          If CheckBox5.Value = True Then CHECKEDcount = CHECKEDcount + 1
          If CheckBox6.Value = True Then CHECKEDcount = CHECKEDcount + 1
          If CheckBox7.Value = True Then CHECKEDcount = CHECKEDcount + 1
          If CheckBox8.Value = True Then CHECKEDcount = CHECKEDcount + 1
          If CheckBox9.Value = True Then CHECKEDcount = CHECKEDcount + 1
          If CheckBox10.Value = True Then CHECKEDcount = CHECKEDcount + 1
          If CHECKEDcount > 1 Then
              If CheckBox1.Value = False Then CheckBox1.Enabled = False
              If CheckBox2.Value = False Then CheckBox2.Enabled = False
              If CheckBox3.Value = False Then CheckBox3.Enabled = False
              If CheckBox4.Value = False Then CheckBox4.Enabled = False
              If CheckBox5.Value = False Then CheckBox5.Enabled = False
              If CheckBox6.Value = False Then CheckBox6.Enabled = False
              If CheckBox7.Value = False Then CheckBox7.Enabled = False
              If CheckBox8.Value = False Then CheckBox8.Enabled = False
              If CheckBox9.Value = False Then CheckBox9.Enabled = False
              If CheckBox10.Value = False Then CheckBox10.Enabled = False
          End If
      End Sub
      Private Sub Enable_All()
              CheckBox1.Enabled = True
              CheckBox2.Enabled = True
              CheckBox3.Enabled = True
              CheckBox4.Enabled = True
              CheckBox5.Enabled = True
              CheckBox6.Enabled = True
              CheckBox7.Enabled = True
              CheckBox8.Enabled = True
              CheckBox9.Enabled = True
              CheckBox10.Enabled = True
      End Sub
      Last edited by Rabbit; Jan 23 '14, 07:43 PM. Reason: Please use [CODE] and [/CODE] tags when posting code or formatted data.

      Comment

      • Luk3r
        Contributor
        • Jan 2014
        • 300

        #4
        Obviously that isn't going to work because you said you only have 6 checkboxes and that code has the potential to enable or disable all 10 checkboxes. I'd like to see your personal code.

        Comment

        • mykill007
          New Member
          • Jan 2014
          • 5

          #5
          I have tried using 10 checkbox. No errors in running. But nothings happening. All I want is when after I Checked 3 out of the 6 Checkbox or 5 out of the 10 in that code the other checkbox will be disable. will be enable back after I unchecked another checkbox

          Comment

          • Luk3r
            Contributor
            • Jan 2014
            • 300

            #6
            Before giving you a full set of working code, I want to explain to you what you need to do. You can try it on your own and get back to this thread, show us your code, and we can go from there. Here are the steps you need to do:

            1) Iterate through your checkboxes to find out which are checked and which are not. (most likely by creating a function to call)

            2) Tell the function what to do when 3 checkboxes are checked and what to do when less than 3 are checked.

            3) Tell each checkbox what to do when it is checked or unchecked and also call the function you wrote.

            Here are a couple of articles to steer you in the right direction:

            Comment

            • mykill007
              New Member
              • Jan 2014
              • 5

              #7
              I've tried topic 2. and I cant seem to make it work.
              Can you just provide me a working code for 6 checkbox and out of the 6 I can only checked 3.
              I'm having trouble understanding the code unless I see a working one.
              Last edited by zmbd; Jan 27 '14, 01:35 PM. Reason: [z{please keep in mind that outright request for code without providing your own work first is discouraged!}]

              Comment

              • Luk3r
                Contributor
                • Jan 2014
                • 300

                #8
                Here is fully functional source code for your request. If you need help with changes to it, please open another thread asking your new question. Enjoy!
                Code:
                Public Class Form1
                    Dim checkCount As Integer
                
                    Private Sub CheckForChecks()
                        If checkCount = 3 Then
                            Dim allCheckboxes As CheckBox() = {CheckBox1, CheckBox2, CheckBox3, CheckBox4, CheckBox5, CheckBox6}
                            Dim check As CheckBox
                
                            For Each check In allCheckboxes
                                If check.Checked = False Then
                                    check.Enabled = False
                                End If
                            Next
                        ElseIf checkCount < 3 Then
                            CheckBox1.Enabled = True
                            CheckBox2.Enabled = True
                            CheckBox3.Enabled = True
                            CheckBox4.Enabled = True
                            CheckBox5.Enabled = True
                            CheckBox6.Enabled = True
                        End If
                    End Sub
                
                    Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged
                        If CheckBox1.Checked = True Then
                            checkCount = checkCount + 1
                        ElseIf CheckBox1.Checked = False AndAlso checkCount > 0 Then
                            checkCount = checkCount - 1
                        End If
                        Call CheckForChecks()
                    End Sub
                
                    Private Sub CheckBox2_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox2.CheckedChanged
                        If CheckBox2.Checked = True Then
                            checkCount = checkCount + 1
                        ElseIf CheckBox2.Checked = False AndAlso checkCount > 0 Then
                            checkCount = checkCount - 1
                        End If
                        Call CheckForChecks()
                    End Sub
                
                    Private Sub CheckBox3_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox3.CheckedChanged
                        If CheckBox3.Checked = True Then
                            checkCount = checkCount + 1
                        ElseIf CheckBox3.Checked = False AndAlso checkCount > 0 Then
                            checkCount = checkCount - 1
                        End If
                        Call CheckForChecks()
                    End Sub
                
                    Private Sub CheckBox4_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox4.CheckedChanged
                        If CheckBox4.Checked = True Then
                            checkCount = checkCount + 1
                        ElseIf CheckBox4.Checked = False AndAlso checkCount > 0 Then
                            checkCount = checkCount - 1
                        End If
                        Call CheckForChecks()
                    End Sub
                
                    Private Sub CheckBox5_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox5.CheckedChanged
                        If CheckBox5.Checked = True Then
                            checkCount = checkCount + 1
                        ElseIf CheckBox5.Checked = False AndAlso checkCount > 0 Then
                            checkCount = checkCount - 1
                        End If
                        Call CheckForChecks()
                    End Sub
                
                    Private Sub CheckBox6_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox6.CheckedChanged
                        If CheckBox6.Checked = True Then
                            checkCount = checkCount + 1
                        ElseIf CheckBox6.Checked = False AndAlso checkCount > 0 Then
                            checkCount = checkCount - 1
                        End If
                        Call CheckForChecks()
                    End Sub
                End Class

                Comment

                • Luk3r
                  Contributor
                  • Jan 2014
                  • 300

                  #9
                  No. It's VB.NET as that's where you asked your question. It should give you something to work off of, though. You will also want to create your own checkbox events instead of copying code directly into a new project. You also copied the whole project into your Form_Load() event, which is incorrect.

                  Comment

                  • mykill007
                    New Member
                    • Jan 2014
                    • 5

                    #10
                    ok my bad. I was just testing it how it would run. Thanks for you time.

                    Comment

                    • Luk3r
                      Contributor
                      • Jan 2014
                      • 300

                      #11
                      You're welcome, and good luck.

                      Comment

                      • Luk3r
                        Contributor
                        • Jan 2014
                        • 300

                        #12
                        In reference to http://bytes.com/topic/visual-basic-...kboxes-checked

                        1) Restructure the code. Do not place it under your FormLoad event
                        2) Replace AndAlso with And
                        3) Create new events for Checkbox Checked

                        Comment

                        • Luk3r
                          Contributor
                          • Jan 2014
                          • 300

                          #13
                          As I'm not as fluent in VB6 as I am in VB.NET, we may need someone with more knowledge to help you, but in the interim, try this.

                          Replace:
                          Code:
                          Dim check as CheckBox
                          For Each check in allCheckBoxes
                          If check.Checked = False then
                          check.Enabled = False
                          End If
                          Next
                          with:
                          Code:
                          For each checkBox in Me.Controls
                          If TypeOf checkBox is CheckBox then
                          If checkBox.Value = 0 then
                          'enter code here that disables Checkbox in VB6
                          'something like: checkBox.Enabled = False
                          End If
                          End If
                          Next

                          Comment

                          • zmbd
                            Recognized Expert Moderator Expert
                            • Mar 2012
                            • 5501

                            #14
                            mykill007:
                            Please do not use images to post code script.
                            Furthermore, using a thirdparty site doesn't really help us help you as they are mostly unavailable to our Experts while they are at work.

                            Instead, Please cut and paste the script between [CODE] [/CODE] by clicking on the [CODE/] button

                            If you must use an image, please use the advanced button at the bottom of the post box, then use the Manage Attachments button to up load.

                            -good computing practices
                            Finally, please understand, many of us cannot or will not d/l un-requested attachments. This has absolutely nothing to do with who you are... In my case, such d/l are prohibited by my ITSec staff. For many others, it is part of "safe computing/best practices" not to d/l such files. A practical example as to why can be found here http://bytes.com/topic/access/answer...l-ms-products. this unfourtunate Member opened a file from a trusted source and is now haveing all sorts of issues!

                            Comment

                            Working...