本文翻译自:How do I group Windows Form radio buttons?
How can I group the radio buttons in Windows Form application (a lot like 's radiobuttonlist!)?如何在Windows窗体应用程序中对单选按钮进行分组(很像的radiobuttonlist!)?
So I can switch between each case chosen from the options.所以我可以在从选项中选择的每个案例之间切换。
#1楼
参考:/question/98eu/如何分组Windows窗体单选按钮
#2楼
GroupBox
is better.But not only group box, even you can usePanels
(System.Windows.Forms.Panel
).GroupBox
更好。但不仅仅是组合框,甚至可以使用Panels
(System.Windows.Forms.Panel
)。
That is very usefully when you are designing Internet Protocol version 4 setting dialog.(Check it with your pc(windows),then you can understand the behavior)当您设计Internet协议版本4设置对话框时,这非常有用。(使用您的电脑(Windows)检查,然后您可以了解行为)
#3楼
I like the concept of grouping RadioButtons in WPF.我喜欢在WPF中分组RadioButtons的概念。There is a propertyGroupName
that specifies which RadioButton controls are mutually exclusive ( /de-de/library/system.windows.controls.radiobutton.aspx ).有一个属性GroupName
,指定哪些RadioButton控件是互斥的( /de-de/library/system.windows.controls.radiobutton.aspx )。
So I wrote a derived class for WinForms that supports this feature:所以我为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 ponentModel;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);}}}
#4楼
看看将单选按钮放在GroupBox中 。
#5楼
您应该将组的所有单选按钮放在同一容器中,例如GroupBox或Panel。
#6楼
Put all radio buttons for a group in a container object like aPanel
or aGroupBox
.将组中的所有单选按钮放在容器对象(如Panel
或GroupBox
。That will automatically group them together in Windows Forms.这将自动将它们组合在Windows窗体中。