C# WinForms RadioButtonList 不存在?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1015411/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
WinForms RadioButtonList doesn't exist?
提问by Ryan Abbott
I know that WebForms
has a RadioButtonList
control, but I can't find one for WinForms
. What I need is to have 3 RadioButtons grouped together, so that only 1 can be selected at a time. I'm finding that I have to do this through code, which is a pain. Am I just not seeing RadioButtonList
somewhere, or does it really not exist in WinForms
?
我知道WebForms
有一个RadioButtonList
控件,但我找不到WinForms
. 我需要的是将 3 个 RadioButtons 组合在一起,以便一次只能选择 1 个。我发现我必须通过代码来做到这一点,这很痛苦。我只是没有看到RadioButtonList
某个地方,还是真的不存在WinForms
?
采纳答案by Matthew Jones
显然不是。
You can group three RadioButtons together using a GroupBox or a Panel as is done here.
回答by Thomas Levesque
The simple fact that several radio buttons are in the same container makes them mutually exclusive, you don't have to code this behavior yourself. Just put them in a Panel or GroupBox as suggested by Matthew
多个单选按钮位于同一个容器中的简单事实使它们相互排斥,您不必自己编写此行为的代码。只需按照 Matthew 的建议将它们放在 Panel 或 GroupBox 中
回答by Reza Aghaei
If you just want to group radio buttons, it's enough to put them in a container, then they will act like a group, but if you need data-binding like how a ComboBox
or ListBox
or CheckedListBox
works, you need a RadioButtonList
control.
如果你只是想对单选按钮进行分组,把它们放在一个容器中就足够了,那么它们就会像一个组一样工作,但是如果你需要像 a ComboBox
orListBox
或 or 一样CheckedListBox
工作的数据绑定,你需要一个RadioButtonList
控件。
Windows forms doesn't have a built-in RadioButtonList
control. You can create your own control by deriving form ListBox
and making it owner-draw and draw radio buttons yourself. This is the way which CheckedListBox
is created as well.
Windows 窗体没有内置RadioButtonList
控件。您可以通过派生表单ListBox
并使其成为所有者绘制和自己绘制单选按钮来创建自己的控件。这也是CheckedListBox
创造的方式。
This way, the control supports data-binding and will benefit from all features of ListBox
, including DataSource
, SelectedValue
, DisplayMember
, ValueMember
and so on. For example you can simply use it this way:
这样一来,该控件支持数据绑定和将所有的功能中获益ListBox
,其中包括DataSource
,SelectedValue
,DisplayMember
,ValueMember
等等。例如,您可以简单地以这种方式使用它:
this.radioButtonList1.DataSource = peopleTable;
this.radioButtonList1.DisplayMember = "Name";
this.radioButtonList1.ValueMember= "Id";
Or for example for an enum
, simply this way:
或者例如对于 a enum
,只需这样:
this.radioButtonList1.DataSource = Enum.GetValues(typeof(DayOfWeek));
In below image, the second RadioButtonList
is disabled by setting Enabled = false;
:
在下图中,RadioButtonList
通过设置禁用第二个Enabled = false;
:
Also the control supports right to left as well:
控件也支持从右到左:
It also supports multi column:
它还支持多列:
RadioButtonList
单选按钮列表
Here is the source code for control. You can use it like a normal ListBox
by adding items or setting data source with/without using data-binding:
这是控制的源代码。您可以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; }
}
}