第三方控件一个单选按钮列表中的WinForms? [英] Third party controls for a radio button list in WinForms?

查看:96
本文介绍了第三方控件一个单选按钮列表中的WinForms?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可以动态地从对象列表中创建一组单选按钮,任何控制?同样的事情也给CheckedBoxList控制,但相互排斥的选择。 这个问题指出,这种控制本身不存在的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. This question points out this control doesn't exist natively for WinForms but are there any third party controls that do this?

推荐答案

控制供应商不能作出任何钱,这样的控制。下面是一些code,让您开始:

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;
}

这篇关于第三方控件一个单选按钮列表中的WinForms?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆