是否有多种方式来组单选按钮在Windows窗体? [英] Are there multiple ways to group radio buttons in Windows Forms?

查看:150
本文介绍了是否有多种方式来组单选按钮在Windows窗体?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的形式,都应该连几个单选按钮,但我想他们中的一个在A组单独的容器它专门与其他一些相关的投入。这里是一般看:

I have a few radio buttons in my form that should all be connected, but I want one of them in a separate container to group it specifically with some other related inputs. Here is the general look:

有没有办法来组,即使它是在自己的组框?

Is there a way to group the fourth radio button with the other 3 even though it is in its own group box?

推荐答案

其实那个单选按钮,只有当他们是相同的父内相互排斥。虽这么说,我能想到的两种选择。

Fact that radio buttons are mutually exclusive only when they are within same parent. Being said that I can think of two options.


  1. 让他们在同一个父,处理油漆父的事件,并得出自己手动给人的印象是,他们是不同的群体

  2. 您必须手动管理互斥:(

  1. Let them be in same parent, handle the Paint event of the parent and draw yourself manually giving the impression that they are different groups.
  2. You have to manually manage the mutual exclusion :(

单选按钮没有神奇的,他们只是设置的其他电台的的经过属性设置为false幕后。

RadioButtons are no magic, they just set the Checked property of other radio's to false behind the scenes.

private List<RadioButton> radioButtons = new List<RadioButton>();
//Populate this with radios interested, then call HookUpEvents and that should work

private void HookUpEvents()
{
    foreach(var radio in radioButtons)
    {
        radio.CheckedChanged -= PerformMutualExclusion;
        radio.CheckedChanged += PerformMutualExclusion;
    }
}

private void PerformMutualExclusion(object sender, EventArgs e)
{
    Radio senderRadio = (RadioButton)sender;
    if(!senderRadio.Checked)
    {
        return;
    }
    foreach(var radio in radioButtons)
    {
        if(radio == sender || !radio.Checked)
        {
            continue;
        }
        radio.Checked = false;
    }
}

这篇关于是否有多种方式来组单选按钮在Windows窗体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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