Resolved max checkboxes selected?

jhcoelho

Member
Joined
Mar 14, 2023
Messages
6
Programming Experience
Beginner
I have a form that, among other fields, has 6 checkboxes but a maximum of 3 can be selected. How do I solve this question?
 
Solution
You can implement the suggestion above like so:
C#:
private CheckBox[] checkBoxes;

private void Form1_Load(object sender, EventArgs e)
{
    // Create the CheckBox array only once the controls have been created.
    checkBoxes = new[] { checkBox1, checkBox2, checkBox3, checkBox4, checkBox5, checkBox6 };

    // Register the handler for the Checked event. This can be done in the designer instead.
    foreach (var checkBox in checkBoxes)
    {
        checkBox.CheckedChanged += CheckBoxes_CheckedChanged;
    }
}

private void CheckBoxes_CheckedChanged(object sender, EventArgs e)
{
    if (checkBoxes.Count(cb => cb.Checked) == 3)
    {
        foreach (var checkBox in checkBoxes)
        {
            // Enable a field if and only if it is...
I think you mean Checked? Handle CheckedChanged event where you count the number of checked boxes, if count is your limit disable the other boxes using Enabled property, otherwise enable them.
 
You can implement the suggestion above like so:
C#:
private CheckBox[] checkBoxes;

private void Form1_Load(object sender, EventArgs e)
{
    // Create the CheckBox array only once the controls have been created.
    checkBoxes = new[] { checkBox1, checkBox2, checkBox3, checkBox4, checkBox5, checkBox6 };

    // Register the handler for the Checked event. This can be done in the designer instead.
    foreach (var checkBox in checkBoxes)
    {
        checkBox.CheckedChanged += CheckBoxes_CheckedChanged;
    }
}

private void CheckBoxes_CheckedChanged(object sender, EventArgs e)
{
    if (checkBoxes.Count(cb => cb.Checked) == 3)
    {
        foreach (var checkBox in checkBoxes)
        {
            // Enable a field if and only if it is checked.
            checkBox.Enabled = checkBox.Checked;
        }
    }
    else
    {
        foreach (var checkBox in checkBoxes)
        {
            // Enable all fields.
            checkBox.Enabled = true;
        }
    }
}
Instead of using a hard-coded array, you could add the CheckBoxes to a Panel and get them from there on deamnd.
 
Solution
CheckedChanged in above example could also be written like this:
C#:
var limited = checkBoxes.Count(cb => cb.Checked) == 3;

foreach (var checkBox in checkBoxes)
{             
    // Enable if not limited, else disable not checked
    checkBox.Enabled = !limited || checkBox.Checked;
}
 
thank you for helping. I'm a beginner and I have a task to do. the exercise has, among several points, (on the registration page) add 6 checkboxes, only being able to select a maximum of 3. Then I have to present the selected boxes on another page (data) and finally send these elements to word, excel and pdf (I know how to do this) the problem is in limiting the boxes to 3 choices and sending them to the "data" page.
 
Hmmm... When did WinForms get pages? Traditionally, WinForms has windows (aka forms) and dialogs, and tabs or controls within those windows and dialogs.

Anyway the modern approach is to use the same data model between the first form and the second form. The traditional approach was to set public properties on the forms.
 
the problem is in limiting the boxes to 3 choices and sending them to the "data" page.

That's two unrelated things so it's not THE problem. You asked a question and we have answered that. If you have a new question on a different subject then start a new thread for that containing all and only the information relevant to that. As for this question, you have your solution so go ahead and use it. Why are you restating the problem like you haven't even read what we wrote?
 
Maybe our OP is hoping that we would architect a solution for them in our zeal to help.
 
That's two unrelated things so it's not THE problem. You asked a question and we have answered that. If you have a new question on a different subject then start a new thread for that containing all and only the information relevant to that. As for this question, you have your solution so go ahead and use it. Why are you restating the problem like you haven't even read what we wrote?

problem solved, obrigado.
 
Back
Top Bottom