开发者

Radiobutton Class - Need to groub Radiobuttons into group

I have created my own radio button class – namely MyRadioButton, as the built in .NET class did not enlarge effectively. (using this for touch screen)

MyRadioButton Class works well, expect for an issue which I do not know How to resolve - When I have multiple MyRdaioButtons on a form, I can select all of them.... They somehow do not work as they should where when one selects one the others are automatically be deselected.

My code is as fol开发者_JS百科lows:

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

    }

}

If someone could provide me with a solution it would be greatly appreciated, as I am pulling out my hair

Thanks

Jens


You may also consider inheriting your control directly from RadioButton, giving you access to the RadioButton.GroupName property, or you will need to implement this type of functionality yourself as kbrinley has posted.


Have you considered using images on a RadioButton control instead? According to ButtonBase's documentation (which RadioButton inherits from):

To have the derived button control display an image, set the Image property or the ImageList and ImageIndex properties.

Note that I have no idea how you'd do selected/unselected states with images... I imagine the ImageList is related to this.


Since this is your control, you will have to provide the logic for this to act like a radio button.

First, I'd suggest placing all of your Radio buttons into a Container control.

Then, at the beginning OnClick method of your control, use the GetContainerControl method to retrieve the Container object and iterate over all of the Radio buttons in the container and set the Checked property of them to false.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜