Home > Enterprise >  How to check multiple radio buttons at once
How to check multiple radio buttons at once

Time:01-04

I am new to coding, and I am currently taking a course. I am trying to make a color mixer where you select two radio buttons from two different categories. But when I run the program, I can only select one radio button at a time. Here is the code I am using:

private void btnMix_Click(object sender, EventArgs e)
{
    if (radRedFirst.Checked && radRedSecond.Checked)
        this.BackColor = System.Drawing.Color.Red;
    else if (radBlueFirst.Checked && radBlueSecond.Checked)
        this.BackColor = System.Drawing.Color.Blue;
    else if (radYellowFirst.Checked && radYellowSecond.Checked)
        this.BackColor = System.Drawing.Color.Yellow;
    else if ((radRedFirst.Checked && radBlueSecond.Checked) ||
            (radBlueFirst.Checked && radRedSecond.Checked))
        this.BackColor = System.Drawing.Color.Purple;
    else if ((radRedFirst.Checked && radYellowSecond.Checked) ||
            (radYellowFirst.Checked && radRedSecond.Checked))
        this.BackColor = System.Drawing.Color.Orange;
    else if ((radBlueFirst.Checked && radYellowSecond.Checked) ||
        (radYellowFirst.Checked && radBlueSecond.Checked))
        this.BackColor = System.Drawing.Color.Green;
}

What can I do to resolve this problem?

I have tried using Panel, but I don't really understand how they work.

CodePudding user response:

Multiple radio buttons are not supposed to be selected. Only one is supposed to be selected.

You should look at using checkboxes instead, as this will resolve your issue. :)

CodePudding user response:

The nice thing about Winforms is that you can almost always inherit a standard control to make a custom version with the functionality you want. The trick is to try and do it in a way that doesn't upend the expected behavior to the point that your user is frustrated when the UI doesn't match their mental model1 of how it should work.

That said, if you're sure you want to do this I won't stand in your way but must "insist" that the [Control] modifier key be held for the click in keeping with the convention for accessing multiselection when it's available.

With that disclaimer, this RadioButtonMulti class can be swapped out in your designer file.

multiselect Control Click to multiselect.


Looking at the default behavior of a RadioButton as documented here.

You group radio buttons by drawing them inside a container such as a Panel control, a GroupBox control, or a form. All radio buttons that are added directly to a form become one group.

This tells us what exactly what we need to do! We need to temporarily make the clicked button "think" that it doesn't belong to any group. Here's one way that I tested that removes the control from its Parent.Controls collection while it toggles the Checked state.

class RadioButtonMulti : RadioButton
{
    protected override void onm ouseDown(MouseEventArgs mevent)
    {
        if(ModifierKeys.Equals(Keys.Control))
        {
            var parentB4 = Parent;
            try
            {
                parentB4.Controls.Remove(this);
                Checked = !Checked;
            }
            finally
            {
                parentB4.Controls.Add(this);
            }
        }
        else
        {
            var others =
                Parent.Controls
                .Cast<Control>()
                .Where(_ => _ is RadioButtonMulti)
                .Where(_ => !ReferenceEquals(_, this));
            foreach (RadioButtonMulti other in others)
            {
                other.Checked = false;
            }
            base.OnMouseDown(mevent);
        }
    }

Display selection in main form title bar for demonstration purposes only.

    protected override void onm ouseUp(MouseEventArgs mevent)
    {
        base.OnMouseUp(mevent);
        if(Form.ActiveForm != null)
        {
            var group =
                Parent.Controls
                .Cast<Control>()
                .Where(_ => _ is RadioButtonMulti)
                .Where(_ => ((RadioButtonMulti)_).Checked);
            Form.ActiveForm.Text = string.Join(", ", group.Select(_ => _.Text));
        }
    }
}

  1. See also Mental Models and Computer Models pp. 11-12.
  • Related