WinForms RadioButtonList不存在?
-
06-07-2019 - |
题
我知道 WebForms
有一个 RadioButtonList
控件,但我找不到 WinForms
。我需要的是将3个RadioButton组合在一起,这样一次只能选择1个。我发现我必须通过代码执行此操作,这很痛苦。我只是在某处看不到 RadioButtonList
,还是在 WinForms
中确实不存在?
解决方案
显然不是。
其他提示
如果您只想将单选按钮分组,就足以将它们放入容器中,那么它们就像一个组,但如果您需要数据绑定,如 ComboBox
或 ListBox
或 CheckedListBox
有效,你需要一个 RadioButtonList
控件。
Windows窗体没有内置的 RadioButtonList
控件。您可以通过派生表单 ListBox
并自己绘制所有者绘制和绘制单选按钮来创建自己的控件。这也是创建 CheckedListBox
的方式。
这样,控件支持数据绑定,并将受益于 ListBox
的所有功能,包括 DataSource
, SelectedValue
, DisplayMember
, ValueMember
等。例如,你可以这样简单地使用它:
this.radioButtonList1.DataSource = peopleTable;
this.radioButtonList1.DisplayMember = "Name";
this.radioButtonList1.ValueMember= "Id";
或者例如 enum
,就是这样:
this.radioButtonList1.DataSource = Enum.GetValues(typeof(DayOfWeek));
在下图中,通过设置 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; }
}
}
几个单选按钮位于同一个容器中的简单事实使它们互相排斥,您不必自己编写此行为。只需按照Matthew
的建议将它们放在Panel或GroupBox中不隶属于 StackOverflow