WinForms RadioButtonList不存在?

11

我知道 WebForms 有一个 RadioButtonList 控件,但我在 WinForms 中找不到类似的控件。我需要将三个单选按钮分组,以便每次只能选择其中一个。我发现必须通过代码来实现这一点,这让我很烦恼。我是没有看到 RadioButtonList,还是它在 WinForms 中确实不存在?

3个回答

22

如果你只是想将单选按钮分组,将它们放在容器中就足够了,它们会像一组一样运作,但如果你需要数据绑定,比如ComboBoxListBoxCheckedListBox那样,你需要一个RadioButtonList控件。

Windows Forms没有内置的RadioButtonList控件。你可以创建自己的控件,通过派生自ListBox并使其为owner-draw来手动绘制单选按钮。这也是CheckedListBox的创建方式。

这种方式下,该控件支持数据绑定,并且会受益于ListBox的所有功能,包括DataSourceSelectedValueDisplayMemberValueMember等等。例如,你可以这样简单地使用它:

this.radioButtonList1.DataSource = peopleTable; 
this.radioButtonList1.DisplayMember = "Name"; 
this.radioButtonList1.ValueMember= "Id";

或者例如对于一个enum,只需这样:

this.radioButtonList1.DataSource = Enum.GetValues(typeof(DayOfWeek)); 

下图中,第二个RadioButtonList通过设置Enabled = false;而被禁用:

图片描述 图片描述

该控件也支持从右到左的排版:

图片描述

它还支持多列:

图片描述

RadioButtonList

以下是该控件的源代码。您可以像正常的ListBox一样添加项目或使用数据绑定设置数据源:

using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using System.Windows.Forms.VisualStyles;
public class RadioButtonList : ListBox
{
    Size s;
    public RadioButtonList()
    {
        this.DrawMode = DrawMode.OwnerDrawFixed;
        using (var g = Graphics.FromHwnd(IntPtr.Zero))
            s = RadioButtonRenderer.GetGlyphSize(
                Graphics.FromHwnd(IntPtr.Zero), RadioButtonState.CheckedNormal);
    }
    protected override void OnDrawItem(DrawItemEventArgs e)
    {

        var text = (Items.Count > 0) ? GetItemText(Items[e.Index]) : Name;
        Rectangle r = e.Bounds; Point p;
        var flags = TextFormatFlags.Default | TextFormatFlags.NoPrefix;
        var selected = (e.State & DrawItemState.Selected) == DrawItemState.Selected;
        var state = selected ?
            (Enabled ? RadioButtonState.CheckedNormal : 
                       RadioButtonState.CheckedDisabled) :
            (Enabled ? RadioButtonState.UncheckedNormal : 
                       RadioButtonState.UncheckedDisabled);
        if (RightToLeft == System.Windows.Forms.RightToLeft.Yes)
        {
            p = new Point(r.Right - r.Height + (ItemHeight - s.Width) / 2,
                r.Top + (ItemHeight - s.Height) / 2);
            r = new Rectangle(r.Left, r.Top, r.Width - r.Height, r.Height);
            flags |= TextFormatFlags.RightToLeft | TextFormatFlags.Right;
        }
        else
        {
            p = new Point(r.Left + (ItemHeight - s.Width) / 2,
            r.Top + (ItemHeight - s.Height) / 2);
            r = new Rectangle(r.Left + r.Height, r.Top, r.Width - r.Height, r.Height);
        }
        var bc = selected ? (Enabled ? SystemColors.Highlight : 
            SystemColors.InactiveBorder) : BackColor;
        var fc = selected ? (Enabled ? SystemColors.HighlightText : 
            SystemColors.GrayText) : ForeColor;
        using (var b = new SolidBrush(bc))
            e.Graphics.FillRectangle(b, e.Bounds);
        RadioButtonRenderer.DrawRadioButton(e.Graphics, p, state);
        TextRenderer.DrawText(e.Graphics, text, Font, r, fc, bc, flags);
        e.DrawFocusRectangle();
        base.OnDrawItem(e);
    }
    [Browsable(false), EditorBrowsable(EditorBrowsableState.Never),
    DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
    public override SelectionMode SelectionMode
    {
        get { return System.Windows.Forms.SelectionMode.One; }
        set { }
    }
    [Browsable(false), EditorBrowsable(EditorBrowsableState.Never),
    DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
    public override int ItemHeight
    {
        get { return (this.Font.Height + 2); }
        set { }
    }
    [EditorBrowsable(EditorBrowsableState.Never), Browsable(false),
    DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
    public override DrawMode DrawMode
    {
        get { return base.DrawMode; }
        set { base.DrawMode = System.Windows.Forms.DrawMode.OwnerDrawFixed; }
    }
}

8

奇怪,我把它们分组放在了一个GroupBox中,但我以为它没有起作用。谢谢! - Ryan Abbott
3
是的,但是这样你就会失去 RadioButtonList 的 Value 属性和数据绑定潜力。 - ProfK

4

多个单选按钮位于同一容器中,它们是互斥的。您不必自己编写代码来实现此行为,只需按照Matthew的建议将它们放在面板或GroupBox中即可。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接