Results 1 to 7 of 7

Thread: Limiting Checkboxes

  1. #1

    Thread Starter
    New Member
    Join Date
    Nov 2012
    Posts
    6

    Question Limiting Checkboxes

    Hi Vbforms,

    I have a question about checkboxes in Visual Basic. I have 6 Checkboxes available to select but i want to limit the amount a user can select to 4. What is the best way to do this? Can i simply grey out the other two after the 4 has been reached?

    Then i want to check if the user has selcted 4 with a button click - How would i check that they have selected 4 not 3, 2 or just the 1

    Currently i'm working on the idea (yes i'm pretty new to VB) but any code examples would be appreciated ^_^
    Thanks

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Limiting Checkboxes

    I don't know which variant of VB you mean, but the VBScript forum is not the right place.

    Based on your previous post I assume VBA, so I have moved this thread to the 'Office Development/VBA' forum.

  3. #3

    Thread Starter
    New Member
    Join Date
    Nov 2012
    Posts
    6

    Re: Limiting Checkboxes

    Hi Mate its not VBA im working on - just straight visual basic in visual studio

    Thanks Phil

  4. #4
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Limiting Checkboxes

    Which version of Visual Studio? (eg: 2010)

  5. #5

    Thread Starter
    New Member
    Join Date
    Nov 2012
    Posts
    6

    Re: Limiting Checkboxes

    Yes 2010 thanks

  6. #6
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Limiting Checkboxes

    In that case... Thread moved to the 'VB.Net' (VB2002 and later) forum

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Limiting Checkboxes

    What do you want to happen if the user tries to check a fifth box? Do you want to prevent it or do you want to uncheck one of the checked ones? I'm guessing the former, so here's a way to disable any unchecked boxes when the maximum number have already been checked and re-enable them if one is unchecked:
    Code:
    Private Const MAX_CHECKED_BOX_COUNT As Integer = 4
    
    Private Sub CheckBoxes_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox6.CheckedChanged,
                                                                                    CheckBox5.CheckedChanged,
                                                                                    CheckBox4.CheckedChanged,
                                                                                    CheckBox3.CheckedChanged,
                                                                                    CheckBox2.CheckedChanged,
                                                                                    CheckBox1.CheckedChanged
        'Get all the CheckBoxes.
        Dim checkBoxes = Controls.OfType(Of CheckBox)().ToArray()
    
        'Get the number of checked CheckBoxes.
        Dim checkedBoxCount = checkBoxes.Count(Function(cb) cb.Checked)
    
        'Unchecked CheckBoxes should be enabled if and only if the number of checked CheckBoxes is less than the maximum number allowed.
        Dim enableUncheckedBoxes = checkedBoxCount < MAX_CHECKED_BOX_COUNT
    
        'Get the unchecked CheckBoxes.
        Dim uncheckedBoxes = checkBoxes.Where(Function(cb) Not cb.Checked)
    
        'Enable or disable the unchecked CheckBoxes as appropriate.
        For Each uncheckedBox In uncheckedBoxes
            uncheckedBox.Enabled = enableUncheckedBoxes
        Next
    End Sub
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width