开发者

How to make usercontrol to behave as radiobutton

i am developing t开发者_JS百科ab button as user control, how can i make it to behave as radiobutton. "When the user selects one option button (also known as a radio button) within a group, the others clear automatically". thank you.


Say your custom Tab button Control is named MyTabButton, Override and implement Equals, and then in the Click event handler in your custom control class,

if (this.Checked)
   foreach(Control myBut in Parent.Controls)
       if (myBut is MyTabButton && !myBut.Equals(this))
          myBut.Checked = false;


If you want the behavior of radio buttons, use radio buttons.

Use javascript to hide the radio buttons and create your tab buttons in place of the original radio buttons. Feed the click events from your tab button to the original radio button. You might also want to have a common event to deselect the other tab buttons.

Your buttons will also degrade nicely if javascript is disabled.

Since you have mentioned you are using winforms, instead of using Javascript, you can override the paint method a derived RadioButton class to paint your Radiobutton as a tab. Here is a basic example

public class ButtonRadioButton : RadioButton {

    protected override void OnPaint(PaintEventArgs e) {
        PushButtonState state;
        if (this.Checked)
            state = PushButtonState.Pressed;
        else
            state = PushButtonState.Normal;

        ButtonRenderer.DrawButton(e.Graphics, e.ClipRectangle, state);
    }

}


obviously you need a container for the button, and whenever a usercontrol is selected, fire the event to container, and container deselect the other usercontrol


Need more information as to if this is WindowsForms, WPF, ASP.NET etc.. but

If it is WPF I have written a post that explains my approach to solving this problem: Grouping and Checkboxes in WPF


Edited to included the answer

You can achieve it by overriding OnClick or OnMouseClick event. I don't get what you mean by "clearing a button", so I just change the Backcolor of it. You can easily adapt it to your Property or other needs.

using System;
using System.Linq;
using System.Windows.Forms;

namespace StackOverflow
{
    public partial class FormMain : Form
    {
        public FormMain()
        {
            InitializeComponent();
        }
    }

    public partial class MyRadioButton : Button
    {
        //Override OnClick event. - THIS IS WHERE ALL THE WORK IS DONE
        protected override void OnClick(EventArgs e)
        {
            do
            {
                /*
                    This is where you select current MyRadioButton. 
                    I'm changing the BackColor for simplicity. 
                */
                this.BackColor = System.Drawing.Color.Green;

                /*
                   If parent of current MyRadioButton is null, 
                   then it doesn't belong in a group.
                */
                if (this.Parent == null)
                    break;

                /*
                    Else loop through all other MyRadioButton of the same group and clear them. 
                    Include System.Linq for this part.
                */
                foreach (MyRadioButton button in this.Parent.Controls.OfType<MyRadioButton>())
                {
                    //If button equals to current MyRadioButton, continue to the next RadioButton
                    if (button == this)
                        continue;

                    //This is where you clear other MyRadioButton
                    button.BackColor = System.Drawing.Color.Red;
                }
            }
            while (false);

            //Continue with the regular OnClick event.
            base.OnClick(e);
        }
    }
}

How to make usercontrol to behave as radiobutton

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜