How do I group Windows Form radio buttons?

前端 未结 9 1563
鱼传尺愫
鱼传尺愫 2020-11-27 11:33

How can I group the radio buttons in Windows Form application (a lot like ASP.NET\'s radiobuttonlist!)?

So I can switch between each case chosen from the options.

相关标签:
9条回答
  • 2020-11-27 12:10

    You should place all the radio buttons of the group inside the same container such as a GroupBox or Panel.

    0 讨论(0)
  • 2020-11-27 12:10

    I like the concept of grouping RadioButtons in WPF. There is a property GroupName that specifies which RadioButton controls are mutually exclusive (http://msdn.microsoft.com/de-de/library/system.windows.controls.radiobutton.aspx).

    So I wrote a derived class for WinForms that supports this feature:

    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);
            }
        }
    }
    
    0 讨论(0)
  • 2020-11-27 12:15

    Put radio buttons inside GroupBox (or other panel)

    0 讨论(0)
提交回复
热议问题