单选按钮类 - 需要将单选按钮分组

时间:2010-01-06 18:20:59

标签: c#

我创建了自己的单选按钮类 - 即MyRadioButton,因为内置的.NET类没有有效扩展。 (用于触摸屏)

MyRadioButton类效果很好,期待一个我不知道的问题如何解决 - 当我在表单上有多个MyRdaioButtons时,我可以选择所有这些......他们不知道应该在哪里工作一个选择一个,其他的被自动取消选择。

我的代码如下:

 public class MyRadioButton : Control
{
    public MyRadioButton()
    {

    }
    private string textTowrite;
    private bool checkStatus;
    private int width;
    private int height;
    public event EventHandler CheckedChanged;
    public delegate void MyHandler1(object sender, EventArgs e);

    protected override void OnClick(EventArgs e)
    {
        base.OnClick(e);
        if (Checked)
            Checked = false;
        else
            Checked = true;
        Invalidate(true);
    }

    protected override void OnPaint(PaintEventArgs e)
    {
        base.OnPaint(e);
        ButtonState btnstate;
        Rectangle rRadioButton;

        if (checkStatus)
        {
            btnstate = ButtonState.Checked;
        }
        else
            btnstate = ButtonState.Normal;

        rRadioButton = new Rectangle(0, 0, RBWidth, RBHeight);
        FontFamily ft = new FontFamily("Tahoma");
        Font fnt_radio = new Font(ft, (int)(18), FontStyle.Bold);

        ControlPaint.DrawRadioButton(e.Graphics, -2, 10, rRadioButton.Width,
            rRadioButton.Height, btnstate);
        //RadioButton's text left justified & centered vertically
        e.Graphics.DrawString(textTowrite, fnt_radio, new SolidBrush(Color.Black), rRadioButton.Right + 1, 16);

    }

    protected virtual void OnCheckedChanged(EventArgs e)
    {
        if (CheckedChanged != null)
        {
            CheckedChanged(this, e);
        }

    }

    public override string Text
    {
        get { return textTowrite; }
        set { textTowrite = value; }
    }

    public bool Checked
    {
        get { return checkStatus; }
        set
        {
            checkStatus = value;
            OnCheckedChanged(EventArgs.Empty);
        }
    }

    public int RBWidth
    {
        get
        {
            if (width == 0)
            {
                width = 40;
            }
            return width;
        }
        set
        {
            if (width != value)
            {
                width = value;
                Invalidate();
            }
        }
    }

    public int RBHeight
    {
        get
        {
            if (height == 0)
            {
                height = 40;
            }
            return height;
        }
        set
        {
            if (height != value)
            {
                height = value;
                Invalidate();
            }
        }

    }

}

如果有人能为我提供解决方案,我会非常感激,因为我正在拔头发

由于

3 个答案:

答案 0 :(得分:1)

您也可以考虑直接从RadioButton继承您的控件,让您可以访问RadioButton.GroupName属性,或者您需要像kbrinley发布的那样自己实现此类功能。

答案 1 :(得分:1)

您是否考虑过在RadioButton控件上使用图片?根据{{​​3}}的文档(ButtonBase继承):

  

具有派生按钮控件   显示图片,设置RadioButton   财产或Image和   ImageList属性。

请注意,我不知道你如何用图像做选择/未选择状态...我想ImageList与此有关。

答案 2 :(得分:0)

由于这是你的控制,你必须提供这个逻辑,就像一个单选按钮。

首先,我建议将所有单选按钮放入Container控件。

然后,在控件的开始OnClick方法中,使用GetContainerControl方法检索Container对象并迭代容器中的所有单选按钮,并将它们的Checked属性设置为假的。