[英]Third party controls for a radio button list in WinForms?
Are there any controls that can dynamically create a group of radio buttons from a list of objects? 是否有任何控件可以根据对象列表动态创建一组单选按钮? Something similar to the CheckedBoxList control, but with mutually exclusive selection.
类似于CheckedBoxList控件,但具有互斥选择。 This question points out this control doesn't exist natively for WinForms but are there any third party controls that do this?
这个问题指出WinForms本身不存在此控件,但是是否有任何第三方控件可以执行此操作?
Maybe; 也许; But this is easier and better to write yourself, though (unless somebody suggests either a free control or better yet sourcecode you can drop into your project).
但这可以使编写自己更容易,更好(除非有人建议您可以使用免费控件或更好的源代码,否则您可以将其放入项目中)。
A little GUI wisdom (I did not make this up but am too lazy to include references): 一点GUI智慧(我没有弥补这一点,但是太懒了以至于无法包含参考):
Of course I gather either you don't have control of that or if you do, won't settle for that answer. 当然,我会收集您可能无法控制的答案,或者您将无法解决该答案。
OK so it looks hard. 好,看起来很难。 So is either squeezing this out of management or doling out cash.
因此,要么将其从管理中挤出来,要么分配现金。
If you want fancier stuff, you can make a usercontrol with labels, checkboxes/radio buttons, etc. in it. 如果您想要更高级的东西,可以在其中添加标签,复选框/单选按钮等的用户控件。 You have to handle selecting/unselecting.
您必须处理选择/取消选择。 Then add the usercontrol to a scrollable panel instead of the radiobutton.
然后,将用户控件而不是单选按钮添加到可滚动面板中。 This provides almost unlimited flexibility.
这提供了几乎无限的灵活性。
Control vendors can't make any money with controls like that. 控件供应商不能用这样的控件赚钱。 Here's some code to get your started:
这是一些入门的代码:
using System;
using System.Drawing;
using System.Windows.Forms;
class RadioList : ListBox {
public event EventHandler SelectedOptionChanged;
public RadioList() {
this.DrawMode = DrawMode.OwnerDrawFixed;
this.ItemHeight += 2;
}
public int SelectedOption {
// Current item with the selected radio button
get { return mSelectedOption; }
set {
if (value != mSelectedOption) {
Invalidate(GetItemRectangle(mSelectedOption));
mSelectedOption = value;
OnSelectedOptionChanged(EventArgs.Empty);
Invalidate(GetItemRectangle(value));
}
}
}
protected virtual void OnSelectedOptionChanged(EventArgs e) {
// Raise SelectOptionChanged event
EventHandler handler = this.SelectedOptionChanged;
if (handler != null) handler(this, e);
}
protected override void OnDrawItem(DrawItemEventArgs e) {
// Draw item with radio button
using (var br = new SolidBrush(this.BackColor))
e.Graphics.FillRectangle(br, e.Bounds);
if (e.Index < this.Items.Count) {
Rectangle rc = new Rectangle(e.Bounds.Left, e.Bounds.Top, e.Bounds.Height, e.Bounds.Height);
ControlPaint.DrawRadioButton(e.Graphics, rc,
e.Index == SelectedOption ? ButtonState.Checked : ButtonState.Normal);
rc = new Rectangle(rc.Right, e.Bounds.Top, e.Bounds.Width - rc.Right, e.Bounds.Height);
TextRenderer.DrawText(e.Graphics, this.Items[e.Index].ToString(), this.Font, rc, this.ForeColor, TextFormatFlags.Left);
}
if ((e.State & DrawItemState.Focus) != DrawItemState.None) e.DrawFocusRectangle();
}
protected override void OnMouseUp(MouseEventArgs e) {
// Detect clicks on the radio button
int index = this.IndexFromPoint(e.Location);
if (index >= 0 && e.X < this.ItemHeight) SelectedOption = index;
base.OnMouseUp(e);
}
protected override void OnKeyDown(KeyEventArgs e) {
// Turn on option with space bar
if (e.KeyData == Keys.Space && this.SelectedIndex >= 0) SelectedOption = this.SelectedIndex;
base.OnKeyDown(e);
}
private int mSelectedOption;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.