C#: How to avoid multiple checked RadioButtons

999 views Asked by At

Under usual circumstances, only one RadioButton can be checked at a time in a Panel.

Now i have a Panel (mainly to achieve vertical scrolling), containing a two-column TableLayoutPanel. New rows can be added infinitely to the TLP by the click on a "Add row"-button (this is why i need the Panel for scrolling down).

Each row in the TLP looks the same:

First column contains another TLP with one col and two rows. First row contains a RadioButton only, second row contains another button only.

Second column contains a CheckedListBox.

So let's say, i have added 5 rows to the TLP. So i have 5 RadioButtons. Don't know why, but the RadioButtons can be checked all at once. How can i avoid that?

Here is the code for adding the rows the the TLP:

    void add_newbox()
    {
        var new_chklistbox = new CheckedListBox{
            Dock=DockStyle.Fill,
            Margin=new Padding(0,0,0,3),
            Location=new Point(20,0),
            Size=new Size(238,94),
            HorizontalScrollbar=true,
            CheckOnClick=true
        };

        var new_radiobutton = new RadioButton{
            Text="",
            Dock=DockStyle.Fill,
            Location=new Point(3,3),
            Size=new Size(14,90),
            MaximumSize=new Size(0,90)
        };
        new_radiobutton.Click += (sender, e) => this.focus=new_chklistbox;

        var new_rembutton = new Button{
            Text="-",
            Dock=DockStyle.Fill,
            AutoSize=true,
            AutoSizeMode=AutoSizeMode.GrowAndShrink,
            Margin=new Padding(0)
        };
        new_rembutton.Click += (sender, e) => rem_items();

        var new_tbl = new TableLayoutPanel{
            RowCount=2,
            ColumnCount=1,
            Dock=DockStyle.Fill,
            Margin=new Padding(0)
        };
        new_tbl.RowStyles.Add(new RowStyle(SizeType.Percent, 70F));
        new_tbl.RowStyles.Add(new RowStyle(SizeType.AutoSize));
        new_tbl.ColumnStyles.Add(new ColumnStyle(SizeType.AutoSize));

        new_tbl.Controls.Add(new_radiobutton,0,0);
        new_tbl.Controls.Add(new_rembutton,0,1);

        tbl_groups.Controls.Add(new_tbl,0,tbl_groups.RowCount);
        tbl_groups.Controls.Add(new_chklistbox,1,tbl_groups.RowCount);
}

Greeting, xola

5

There are 5 answers

0
Reza Aghaei On BEST ANSWER

To have some radio buttons act as a group you should host them all in the same container. Different table layout panels are different container. It describes the behavior.

If for any reason you want to keep the layout as is, you need to set AutoCheck property of those radio buttons to true and handle Click event of them to check just clicked one and uncheck rest of them.

Example

private void Form1_Load(object sender, EventArgs e)
{
    var radios = GetChildren(tableLayoutPanel1).OfType<RadioButton>();
    foreach (var radio in radios)
    {
        radio.AutoCheck=false;
        radio.Click += (obj, arg) =>
        {
            radio.Checked = true;
            foreach(var r in radios)
                if (r != radio)
                    r.Checked = false;
        };
    }
}

IEnumerable<Control> GetChildren(Control control)
{
    foreach (Control c1 in control.Controls)
    {
        yield return c1;
        foreach (Control c2 in GetChildren(c1))
            yield return c2;
    }
}
1
Manju On

Use Radiobutton's GroupName property.

0
greyhame On
 <RadioButton Content="{content}" GroupName="RadioGroupIdentifier"/>

Is what you are looking for I guess.

EDIT

Didn't notice you were referring to win forms (you could have wrote it in the question since tags are often skipped while reading).

However, you should read this article from microsoft , here is the useful part:

  1. Drag a GroupBox or Panel control from the Windows Forms tab on the Toolbox onto the form.
  2. Draw RadioButton controls on the GroupBox or Panel control.

In general every RadioButton is grouped inside the parent GroupBox or Panel by default.

There is no other easy way to do this just via designer. You could of course implement your own logic via events buuuut.... I wouldn't recommend that.

1
Manju On

Try this. It might work for you.

void add_newbox()
{
    var new_chklistbox = new CheckedListBox{
        Dock=DockStyle.Fill,
        Margin=new Padding(0,0,0,3),
        Location=new Point(20,0),
        Size=new Size(238,94),
        HorizontalScrollbar=true,
        CheckOnClick=true
    };

    var new_radiobutton = new RadioButton{
        Text="",
        Dock=DockStyle.Fill,
        Location=new Point(3,3),
        Size=new Size(14,90),
        MaximumSize=new Size(0,90)
    };
    //new_radiobutton.Click += (sender, e) => this.focus=new_chklistbox;

    var new_rembutton = new Button{
        Text="-",
        Dock=DockStyle.Fill,
        AutoSize=true,
        AutoSizeMode=AutoSizeMode.GrowAndShrink,
        Margin=new Padding(0)
    };
    //new_rembutton.Click += (sender, e) => rem_items();

    var new_tbl = new TableLayoutPanel{
        RowCount=2,
        ColumnCount=1,
        Dock=DockStyle.Fill,
        Margin=new Padding(0)
    };
    var new_panel = new Panel
    {
        Dock = DockStyle.Fill,
        AutoSize = true,
        Margin = new Padding(0)
    };
    new_panel.Controls.Add(new_radiobutton);
    new_panel.Controls.Add(new_rembutton);
    new_tbl.RowStyles.Add(new RowStyle(SizeType.Percent, 70F));
    new_tbl.RowStyles.Add(new RowStyle(SizeType.AutoSize));
    new_tbl.ColumnStyles.Add(new ColumnStyle(SizeType.AutoSize));

    new_tbl.Controls.Add(new_panel, 0, 0);
0
Sandeep On

I have made few changes in your code and commented old code. Please try that. In this code I am adding new row in existing TPL control rather than adding new TLP every time.

    void add_newbox()
    {
        var new_chklistbox = new CheckedListBox
        {
            Dock = DockStyle.Fill,
            Margin = new Padding(0, 0, 0, 3),
            Location = new Point(20, 0),
            Size = new Size(238, 94),
            HorizontalScrollbar = true,
            CheckOnClick = true
        };

        var new_radiobutton = new RadioButton
        {
            Text = "",
            Dock = DockStyle.Fill,
            Location = new Point(3, 3),
            Size = new Size(14, 90),
            MaximumSize = new Size(0, 90)
        };
        new_radiobutton.Click += (sender, e) => this.focus = new_chklistbox;

        var new_rembutton = new Button
        {
            Text = "-",
            Dock = DockStyle.Fill,
            AutoSize = true,
            AutoSizeMode = AutoSizeMode.GrowAndShrink,
            Margin = new Padding(0)
        };
        new_rembutton.Click += (sender, e) => rem_items();

        //var new_tbl = new TableLayoutPanel
        //{
        //    RowCount = 2,
        //    ColumnCount = 1,
        //    Dock = DockStyle.Fill,
        //    Margin = new Padding(0)
        //};
        //new_tbl.RowStyles.Add(new RowStyle(SizeType.Percent, 70F));
        //new_tbl.RowStyles.Add(new RowStyle(SizeType.AutoSize));
        //new_tbl.ColumnStyles.Add(new ColumnStyle(SizeType.AutoSize));

        //new_tbl.Controls.Add(new_radiobutton, 0, 0);
        //new_tbl.Controls.Add(new_rembutton, 0, 1);

        tbl_groups.RowCount = tbl_groups.RowCount + 1;
        tbl_groups.RowStyles.Add(new RowStyle(SizeType.AutoSize));
        tbl_groups.ColumnStyles.Add(new ColumnStyle(SizeType.AutoSize));

        tbl_groups.Controls.Add(new_radiobutton, 0, tbl_groups.RowCount - 1);
        tbl_groups.Controls.Add(new_rembutton, 1, tbl_groups.RowCount - 1);
    }