如何将嵌套组框中的单选按钮设置为与该组框外的单选按钮相同的组单选、按钮、嵌套、设置为

2023-09-06 22:13:23 作者:叱咤風雲

我有 winform 应用程序 (.NET 4.0)

I have winform application (.NET 4.0)

有没有办法手动设置一组单选按钮?

Is there any way to manually set a group of radio buttons?

我有四个单选按钮,其中两个在组框内,另外两个在该框外.如何将它们全部设置到同一个组?

I have four radio buttons, two of them inside of a group box and the other two outside of that box. How can I set all of them to the same group?

推荐答案

这可能已经在另一个帖子中回答了,听起来一样:

This might have been answered in another post, it sounds the same:

将 Windows 窗体单选按钮分组到不同的父级C#中的控件

这是公认的解决方案:

恐怕您将不得不手动处理此问题...还不错实际上,您可能只需将所有 RadioButton 存储在一个列表中,并为所有这些使用单个事件处理程序:

I'm afraid you'll have to handle this manually... It's not so bad actually, you can probably just store all the RadioButton in a list, and use a single event handler for all of them:

private List<RadioButton> _radioButtonGroup = new List<RadioButton>();
private void radioButton_CheckedChanged(object sender, EventArgs e)
{
    RadioButton rb = (RadioButton)sender;
    if (rb.Checked)
    {
        foreach(RadioButton other in _radioButtonGroup)
        {
            if (other == rb)
            {
                continue;
            }
            other.Checked = false;
        }
    }
}

这是另一个问同样问题的问题:不同面板中的单选按钮组

Here's another question asking the same thing: Radiobuttons as a group in different panels