Avatar of pythonV
pythonV asked on

VB.NET 2003 - FORCING USER TO CLICK A PRE-DETERMINED NUMBER OF CHECKBOXES ONLY

Hi,

I dropped a Groupbox onto my form, and dropped 5 Checkboxes in it.

The idea is to put code in the Groupbox which forces the user to choose 1 or 2 checkboxes only from a possible 5. I want the code to disallow the user from proceeding if he does not check at least one box or checks 3 or more. The Groupbox also has a label which will display the message "Choose 1 or 2 Boxes" until these conditions are met by the user.

in other words, the user won't be allowed to click the Button (which performs another unrelated operation) but will be stuck in the Groupbox until he meets conditions.

The following is the Code View:

Private Sub GroupBox1_Enter(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GroupBox1.Enter
        Dim checkMax As Int16

        If CheckBox1.Checked = True Then
            checkMax = (checkMax + 1)
        End If
        If CheckBox2.Checked = True Then
            checkMax = (checkMax + 1)
        End If
        If CheckBox3.Checked = True Then
            checkMax = (checkMax + 1)
        End If
        If CheckBox4.Checked = True Then
            checkMax = (checkMax + 1)
        End If
        If CheckBox5.Checked = True Then
            checkMax = (checkMax + 1)
        End If

        If checkMax = 0 Or checkMax > 2 Then
            Label1.Text = "You can only check 1 or 2 boxes"
        Else : Label1.Text = ""
        End If

    End Sub



Visual Basic.NET

Avatar of undefined
Last Comment
pythonV

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
jppinto

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
ASKER
pythonV

Hi Jp,

Thanks for the solution but the following would still be required to make it work correctly:

If the user clicks 3 checkboxes the button becomes disabled... ok up to here. But it doesn't get enabled again when the third checkbox is unticked.

And when the user hasn't yet clicked a checkbox, the button is still enabled even though 0 choices is not a valid choice.

Many thanks
pythonV

ASKER
pythonV

Hi Jp,

Your solution works fine if I  just add the third line to the first two supplied by you:
If checkMax = 0 Or checkMax > 2 Then
            Button1.Enabled = False
        Else : Button1.Enabled = True

Many thanks for your assistance,
pythonV
ASKER
pythonV

Many thanks

pythonV
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck