Rya*_*ott 7 c# radiobuttonlist radio-button winforms
我知道WebForms
有一个RadioButtonList
控件,但我找不到一个WinForms
.我需要的是将3个RadioButton组合在一起,这样一次只能选择1个.我发现我必须通过代码执行此操作,这很痛苦.我只是没有看到RadioButtonList
某个地方,或者它真的不存在WinForms
?
Rez*_*aei 11
如果你只想组合单选按钮,它就足以将它们放在一个容器中,那么它们就像一个组一样,但是如果你需要数据绑定,就像一个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";
Run Code Online (Sandbox Code Playgroud)
或者例如对于一个enum
,简单地这样:
this.radioButtonList1.DataSource = Enum.GetValues(typeof(DayOfWeek));
Run Code Online (Sandbox Code Playgroud)
在下面的图像中,RadioButtonList
通过设置禁用第二个Enabled = false;
:
此外,控件也支持从右到左:
它还支持多列:
这是控制的源代码.通过使用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; }
}
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
18900 次 |
最近记录: |