简体   繁体   English

C#:如何避免多个选中的单选按钮

[英]C#: How to avoid multiple checked RadioButtons

Under usual circumstances, only one RadioButton can be checked at a time in a Panel. 在通常情况下,面板中一次只能检查一个RadioButton。

Now i have a Panel (mainly to achieve vertical scrolling), containing a two-column TableLayoutPanel. 现在,我有一个Panel(主要用于实现垂直滚动),其中包含一个两列的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). 通过单击“添加行”按钮,可以将新行无限地添加到TLP(这就是为什么我需要面板向下滚动的原因)。

Each row in the TLP looks the same: TLP中的每一行看起来都一样:

First column contains another TLP with one col and two rows. 第一列包含具有一个列和两行的另一个TLP。 First row contains a RadioButton only, second row contains another button only. 第一行仅包含一个RadioButton,第二行仅包含另一个按钮。

Second column contains a CheckedListBox. 第二列包含一个CheckedListBox。

So let's say, i have added 5 rows to the TLP. 所以说,我向TLP添加了5行。 So i have 5 RadioButtons. 所以我有5个RadioButtons。 Don't know why, but the RadioButtons can be checked all at once. 不知道为什么,但是可以一次检查RadioButtons。 How can i avoid that? 我如何避免这种情况?

Here is the code for adding the rows the the TLP: 这是用于在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 问候,希拉

使用单选按钮的GroupName属性。

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);

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. 如果出于任何原因要保留原样,则需要将那些单选按钮的AutoCheck属性设置为true并处理它们的Click事件,以检查刚刚单击的按钮并取消选中其余按钮。

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;
    }
}

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. 在这段代码中,我将在现有的TPL控件中添加新行,而不是每次都添加新的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.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);
    }
 <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: 但是,您应该从microsoft阅读本文 ,这里是有用的部分:

  1. Drag a GroupBox or Panel control from the Windows Forms tab on the Toolbox onto the form. 将GroupBox或Panel控件从“工具箱”上的“ Windows窗体”选项卡拖动到窗体上。
  2. Draw RadioButton controls on the GroupBox or Panel control. 在GroupBox或Panel控件上绘制RadioButton控件。

In general every RadioButton is grouped inside the parent GroupBox or Panel by default. 通常,默认情况下,每个RadioButton都分组在父GroupBox或Panel中。

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. 您当然可以通过事件buuuut实现自己的逻辑。...我不建议这样做。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM