如何将Windows窗体单选button分组?
如何将Windows窗体应用程序中的单选button(很像ASP.NET的单选button!)分组?
所以我可以切换从选项中select的每个案例。
将组中的所有单选button放在容器对象(如Panel
或GroupBox
。 这将自动在Windows窗体中将它们分组在一起。
看看把你的单选button放在一个GroupBox中 。
您应该将组中的所有单选button放在同一个容器(如GroupBox或Panel)中。
我喜欢在WPF中对RadioButton进行分组的概念。 有一个属性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); } } }
没有面板的单选button
public class RadioButton2 : RadioButton { public string GroupName { get; set; } } private void RadioButton2_Clicked(object sender, EventArgs e) { RadioButton2 rb = (sender as RadioButton2); if (!rb.Checked) { foreach (var c in Controls) { if (c is RadioButton2 && (c as RadioButton2).GroupName == rb.GroupName) { (c as RadioButton2).Checked = false; } } rb.Checked = true; } } private void Form1_Load(object sender, EventArgs e) { //a group RadioButton2 rb1 = new RadioButton2(); rb1.Text = "radio1"; rb1.AutoSize = true; rb1.AutoCheck = false; rb1.Top = 50; rb1.Left = 50; rb1.GroupName = "a"; rb1.Click += RadioButton2_Clicked; Controls.Add(rb1); RadioButton2 rb2 = new RadioButton2(); rb2.Text = "radio2"; rb2.AutoSize = true; rb2.AutoCheck = false; rb2.Top = 50; rb2.Left = 100; rb2.GroupName = "a"; rb2.Click += RadioButton2_Clicked; Controls.Add(rb2); //b group RadioButton2 rb3 = new RadioButton2(); rb3.Text = "radio3"; rb3.AutoSize = true; rb3.AutoCheck = false; rb3.Top = 80; rb3.Left = 50; rb3.GroupName = "b"; rb3.Click += RadioButton2_Clicked; Controls.Add(rb3); RadioButton2 rb4 = new RadioButton2(); rb4.Text = "radio4"; rb4.AutoSize = true; rb4.AutoCheck = false; rb4.Top = 80; rb4.Left = 100; rb4.GroupName = "b"; rb4.Click += RadioButton2_Clicked; Controls.Add(rb4); }
GroupBox
比较好。但是不仅是组框,甚至可以使用Panels
( System.Windows.Forms.Panel
)。
- 当你devise互联网协议版本4设置对话框时,这是非常有用的(用你的电脑(windows)检查它,你就可以理解这个行为)
如果你不能把它们放到一个容器中,那么你必须编写代码来改变每个RadioButton的 检查状态:
private void rbDataSourceFile_CheckedChanged(object sender, EventArgs e) { rbDataSourceNet.Checked = !rbDataSourceFile.Checked; } private void rbDataSourceNet_CheckedChanged(object sender, EventArgs e) { rbDataSourceFile.Checked = !rbDataSourceNet.Checked; }
将单选button放在GroupBox (或其他面板)