Winforms 如何将自定义控件中的单选按钮组合在一起?

Winforms 如何将自定义控件中的单选按钮组合在一起?,winforms,c#-4.0,radio-button,custom-controls,grouping,Winforms,C# 4.0,Radio Button,Custom Controls,Grouping,我有一个自定义控件(不是组件),它基于一个单选按钮,该按钮有一个奇特的标签,当标签被选中时,它会发出绿色的光,当它不能提供更好的视觉反馈时,它会发出黑色的光 为了证实我的恐惧,我在一张表格上放了一堆,然后运行它,果然,它们没有被认为是“组合在一起”,因为我可以激活所有这些,而没有一个被停用 我如何才能使这些控件都成为“特殊单选按钮组”的一部分,并像普通单选按钮一样工作?好的,所以我找到了“一个”解决方案。我不知道这是否是一个理想的解决方案,但它是有效的 我关闭了查找“CheckedChanged

我有一个自定义控件(不是组件),它基于一个单选按钮,该按钮有一个奇特的标签,当标签被选中时,它会发出绿色的光,当它不能提供更好的视觉反馈时,它会发出黑色的光

为了证实我的恐惧,我在一张表格上放了一堆,然后运行它,果然,它们没有被认为是“组合在一起”,因为我可以激活所有这些,而没有一个被停用

我如何才能使这些控件都成为“特殊单选按钮组”的一部分,并像普通单选按钮一样工作?

好的,所以我找到了“一个”解决方案。我不知道这是否是一个理想的解决方案,但它是有效的

我关闭了查找“CheckedChanged”事件的对象函数。问题是,无论是手动还是编程,这都会触发。 解决方案使用“单击”事件。但不仅仅是在按钮上,整个事情上

所以我们得到的是:

namespace Pieces{
public partial class ucGraphicLabelRadioButton : UserControl{
    private event EventHandler _CheckedChanged;

    /// <summary>
    /// Gets or sets the controls text.
    /// </summary>
    public override string Text{
        get{return this.lblTitle.Text;}
        set{lblTitle.Text = value;}
    }
    /// <summary>
    /// Gets or sets the checked state of the button.
    /// </summary>
    public bool Checked{
        get{return this.rbtnButton.Checked;}
        set{this.rbtnButton.Checked = value;}
    }

    public event EventHandler CheckedChanged{
        add{this._CheckedChanged += value;}
        remove{this._CheckedChanged -= value;}
    }

    public ucGraphicLabelRadioButton(){
        InitializeComponent();
    }

    //This is where the fancy stuff happens...
    private void ToggleCheck(object sender, EventArgs e){
        this.lblTitle.GlowColor = Color.Green;
        bool FoundOtherChecked = false;
        this.Parent.Controls.OfType<ucGraphicLabelRadioButton>().Where(x => x.Checked && x != this).ToList().ForEach(x => {
            x.Checked = false;
            x.lblTitle.GlowColor = Color.Black;
            FoundOtherChecked = true;
        });

        if ((FoundOtherChecked && !this.Checked) || !this.Checked){
            this.Checked = !this.Checked;            
            this.lblTitle.GlowColor = this.rbtnButton.Checked ? Color.Green : Color.Black;
        }

        if (this._CheckedChanged != null)
            this._CheckedChanged(this, new EventArgs());
    }
}
这完全是过火了,因为搜索只会返回单个对象(或者在关闭自身的情况下不返回任何对象),但它确实有效。有更好的方法吗


编辑:我必须做一些更改,因为显然,当您直接单击单选按钮时,它会先被选中,然后单击事件就会触发。此外,要确保它们的行为更像单选按钮,而不像具有单选按钮行为的复选框,即:用户现在不能单击它们。它会先检查一个是否打开,然后再将其关闭。

WinForms?你是从RadioButton继承的吗?我没有。如果我有,我知道我可以让它工作,但我用一个特殊的标签(gLabel)的标准标签,不知道如何奠定了标准标签。这可能是一个理想的解决方案。。。
.ToList().ForEach(x => ...)