如何将嵌套组框中的单选按钮设置为具有该组框外的单选按钮的同一组 [英] How to set radio buttons in a nested group box as a same group with radio buttons outside of that group box

查看:58
本文介绍了如何将嵌套组框中的单选按钮设置为具有该组框外的单选按钮的同一组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 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

这篇关于如何将嵌套组框中的单选按钮设置为具有该组框外的单选按钮的同一组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆