[英]C# WinForms Radio Buttons
我加载了4个无线电按钮,当我运行程序并单击它们时,如果我单击一个,然后单击另一个,其他的就会消失。 如果我想要两个单选按钮但是他们做了不同的事情怎么办?
将不同的选择集分组到单独的组框(或面板或其他容器控件中,但组框可能是您所追求的)。
MSDN :
Windows窗体RadioButton控件旨在为用户提供两个或更多设置的选择,其中只有一个可以分配给过程或对象。 例如,一组RadioButton控件可以显示订单的包裹载体的选择,但是将仅使用一个载波。 因此,即使它是功能组的一部分,也可以一次只选择一个RadioButton。
您可以通过在容器(如Panel控件,GroupBox控件或表单)中绘制单选按钮来对其进行分组。
假设您的表单上有四个或更多单选按钮。 如果您只有两个,比如说,并且您希望允许用户同时选择两者,请使用一组复选框。
您应该使用GroupBox控件。
GroupBox中的所有单选按钮都是互斥的 - 在一个GroupBox中放置2个单选按钮,在另一个GroupBox中放置另外2个单选按钮。
如果您不希望容器可见 - 请使用Panel控件而不是GroupBox。 (只需拖放)
正如您可能猜到的那样,上面的一个是GroupBox,下面的一个(它是不可见的,但只允许选择其中的一个readioButton)是一个面板。
HTH。
我喜欢在WPF中分组RadioButtons的概念。 有一个属性GroupName
,指定哪些RadioButton控件是互斥的( http://msdn.microsoft.com/de-de/library/system.windows.controls.radiobutton.aspx )。
所以我为WinForms写了一个支持这个功能的派生类:
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<Control> GetAll(Control control, Type type)
{
var controls = control.Controls.Cast<Control>();
return controls.SelectMany(ctrl => GetAll(ctrl, type))
.Concat(controls)
.Where(c => c.GetType() == type);
}
}
}
尝试在容器中放置类似的选项,例如GroupBox
通常,当仅应用一个选项时,使用一组单选按钮。 如果同时选择多个选项有效,请改用复选框。
将所有单选按钮放入组框中。
现在,在所有单选按钮属性上,将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;
}
这应该使那些选定的单选按钮检查。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.