C# 如何在 Windows 窗体中对单选按钮组进行分组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10944679/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 15:52:55  来源:igfitidea点击:

How to group Groups of radio buttons in Windows forms

c#radio-buttonwindows-forms-designer

提问by Ulrik

I have a form with a number of radiobuttons where only one should be selected at a time.

我有一个包含许多单选按钮的表单,一次只能选择一个。

Some of the radiobuttons are connected and need to bee explained by a header. For this i put them in a Groupbox. But then is the radiobuttons inside the groupbox no longer connected to the ones outside and its possible to select two off them.

一些单选按钮是连接的,需要用标题来解释。为此,我将它们放在 Groupbox 中。但是,分组框内的单选按钮不再连接到外面的单选按钮,并且可以从它们中选择两个。

Is there a way to connect the radiobuttons so they all react on eachother? Or is there a better way to group and explain the radiobuttons then using a groupbox?

有没有办法连接单选按钮,让它们都对彼此做出反应?或者有没有更好的方法来分组和解释单选按钮然后使用分组框?

采纳答案by Mark Hall

It is the designed behavior of a RadioButtonto be grouped by its container according to MSDN:

根据 MSDN,它是按容器分组的RadioButton的设计行为:

When the user selects one option button (also known as a radio button) within a group, the others clear automatically. All RadioButton controls in a given container, such as a Form, constitute a group. To create multiple groups on one form, place each group in its own container, such as a GroupBox or Panel control

当用户选择一组中的一个选项按钮(也称为单选按钮)时,其他选项会自动清除。给定容器(例如 Form)中的所有 RadioButton 控件构成一个组。要在一个窗体上创建多个组,请将每个组放在其自己的容器中,例如 GroupBox 或 Panel 控件

You could try using a common eventhandler for the RadioButtons that you want linked and handle the Checking/UnChecking yourself, Or you can place your RadioButtons on top of your GroupBox, not adding them to the GroupBox then BringToFront.

您可以尝试为要链接的 RadioButtons 使用通用事件处理程序并自己处理 Checking/UnChecking,或者您可以将 RadioButtons 放在 GroupBox 的顶部,而不是将它们添加到 GroupBox 然后BringToFront。

回答by Me.Name

In windows forms I don't think there is an easy way (as in setting a property such as GroupName) to group radiobuttons. The quickest way would be to simply group the radiobuttons in a collection and listen to the checkedchanged event. For example:

在 Windows 窗体中,我认为没有一种简单的方法(如设置 GroupName 等属性)来对单选按钮进行分组。最快的方法是简单地将单选按钮分组到一个集合中,然后监听 checkchanged 事件。例如:

        var rbuttons = new List<RadioButton>{ radioButton1, radioButton2, radioButton3, radioButton4 }; //or get them dynamically..
        rbuttons.ForEach(r => r.CheckedChanged += (o, e) =>
        {
            if (r.Checked) rbuttons.ForEach(rb => rb.Checked = rb == r);
        });

If any of the radiobuttons in the list is checked, the others are automatically unchecked

如果选中列表中的任何单选按钮,则其他单选按钮将自动取消选中

回答by Hans Passant

This is possible, but it comes at a high price. You'll have to set their AutoCheck property to false and take care of unchecking other buttons yourself. The most awkward thing that doesn't work right anymore is tab stops, a grouped set of buttons has only one tabstop but if you set AutoCheck = false then every button can be tabbed to.

这是可能的,但代价高昂。您必须将其 AutoCheck 属性设置为 false 并自行取消选中其他按钮。不再正常工作的最尴尬的事情是制表位,一组按钮只有一个制表位,但是如果您设置 AutoCheck = false 那么每个按钮都可以被制表符。

The biggest problem no doubt it is the considerable confusion you'll impart on the user. So much so that you probably ought to consider checkboxes instead.

毫无疑问,最大的问题是您会给用户带来相当大的困惑。如此之多以至于您可能应该考虑使用复选框。

回答by tda

I solved this by handling the CheckChanged event.

我通过处理 CheckChanged 事件解决了这个问题。

private void radio1_CheckedChanged(object sender, EventArgs e)
{
    if (radio1.Checked)
    {
        // do stuff
        radio2.Checked = false;
    }
}
private void radio2_CheckedChanged(object sender, EventArgs e)
{
    if (radio2.Checked)
    {
        // do stuff
        radio1.Checked = false;
    }
}