C#WinForms单选按钮

我加载了4个无线电按钮,当我运行程序并单击它们时,如果我单击一个,然后单击另一个,其他的就会消失。 如果我想要两个单选按钮但是他们做了不同的事情怎么办?

将不同的选择集分组到单独的组框(或面板或其他容器控件中,但组框可能是您所追求的)。

MSDN :

Windows窗体RadioButton控件旨在为用户提供两个或多个设置的选择,其中只有一个可以分配给过程或对象。 例如,一组RadioButton控件可以显示订单的包裹载体的选择,但是将仅使用一个载波。 因此,即使它是function组的一部分,也可以一次只选择一个RadioButton。

您可以通过在容器(如Panel控件,GroupBox控件或表单)中绘制单选按钮来对其进行分组。

假设您的表单上有四个或更多单选按钮。 如果您只有两个,比如说,并且您希望允许用户选择两者,请使用一组复选框。

您应该使用GroupBox控件。
GroupBox中的所有单选按钮都是互斥的 – 在一个GroupBox中放置2个单选按钮,在另一个GroupBox中放置另外2个单选按钮。
如果您不希望容器可见 – 请使用Panel控件而不是GroupBox。 (只需拖放)

替代文字

正如您可能猜到的那样,上面的一个是GroupBox,下面的一个(它是不可见的,但只允许选择其中的一个readioButton)是一个面板。

HTH。

尝试在容器中放置类似的选项,例如GroupBox

通常,当仅应用一个选项时,使用一组单选按钮。 如果同时选择多个选项有效,请改用复选框。

我喜欢在WPF中分组RadioButtons的概念。 有一个属性GroupName ,指定哪些RadioButton控件是互斥的( http://msdn.microsoft.com/de-de/library/system.windows.controls.radiobutton.aspx )。

所以我为WinForms写了一个支持这个function的派生类:

 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows.Forms; using System.Diagnostics; using System.Windows.Forms.VisualStyles; using System.Drawing; using System.ComponentModel; namespace Use.your.own { public class AdvancedRadioButton : CheckBox { public enum Level { Parent, Form }; [Category("AdvancedRadioButton"), Description("Gets or sets the level that specifies which RadioButton controls are affected."), DefaultValue(Level.Parent)] public Level GroupNameLevel { get; set; } [Category("AdvancedRadioButton"), Description("Gets or sets the name that specifies which RadioButton controls are mutually exclusive.")] public string GroupName { get; set; } protected override void OnCheckedChanged(EventArgs e) { base.OnCheckedChanged(e); if (Checked) { var arbControls = (dynamic)null; switch (GroupNameLevel) { case Level.Parent: if (this.Parent != null) arbControls = GetAll(this.Parent, typeof(AdvancedRadioButton)); break; case Level.Form: Form form = this.FindForm(); if (form != null) arbControls = GetAll(this.FindForm(), typeof(AdvancedRadioButton)); break; } if (arbControls != null) foreach (Control control in arbControls) if (control != this && (control as AdvancedRadioButton).GroupName == this.GroupName) (control as AdvancedRadioButton).Checked = false; } } protected override void OnClick(EventArgs e) { if (!Checked) base.OnClick(e); } protected override void OnPaint(PaintEventArgs pevent) { CheckBoxRenderer.DrawParentBackground(pevent.Graphics, pevent.ClipRectangle, this); RadioButtonState radioButtonState; if (Checked) { radioButtonState = RadioButtonState.CheckedNormal; if (Focused) radioButtonState = RadioButtonState.CheckedHot; if (!Enabled) radioButtonState = RadioButtonState.CheckedDisabled; } else { radioButtonState = RadioButtonState.UncheckedNormal; if (Focused) radioButtonState = RadioButtonState.UncheckedHot; if (!Enabled) radioButtonState = RadioButtonState.UncheckedDisabled; } Size glyphSize = RadioButtonRenderer.GetGlyphSize(pevent.Graphics, radioButtonState); Rectangle rect = pevent.ClipRectangle; rect.Width -= glyphSize.Width; rect.Location = new Point(rect.Left + glyphSize.Width, rect.Top); RadioButtonRenderer.DrawRadioButton(pevent.Graphics, new System.Drawing.Point(0, rect.Height / 2 - glyphSize.Height / 2), rect, this.Text, this.Font, this.Focused, radioButtonState); } private IEnumerable GetAll(Control control, Type type) { var controls = control.Controls.Cast(); return controls.SelectMany(ctrl => GetAll(ctrl, type)) .Concat(controls) .Where(c => c.GetType() == type); } } } 

将所有单选按钮放入组框中。

现在,在所有单选按钮属性上,将Auto Check设置为false

或者,您可以设置

 radioButton1->AutoCheck = false; 

radioButton1_Click函数内,根据需要处理。 例:

 Void radioButton1_Click(Object^ sender, EventArgs^ e) { radioButton1->Enabled = true; radioButton2->Enabled = true; radioButton3->Enabled = true; radioButton1->Checked = true; radioButton2->Checked = true; radioButton3->Checked = true; } 

这应该使那些选定的单选按钮检查。