简体   繁体   中英

Radio button CheckedChanged event only when checked?

Situation

I want to have an event for when a radiobutton is checked only.

I know that the event CheckedChanged event is called when a radio button is checked and unchecked and I know why. I know how to use it to only have code execution when it is checked. See : Radio button checked changed event fires twice

Question

However I worked with others languages which had event for when the radio button is checked (and not called when unchecked). Is there such an event that directly do this in C#

(it's mostly a yes or no question to know if there is a specific event for this, since I'm well aware I can make a check in the event) ?

Not to my knowledge. You'd be best off doing something like this:

var radioButton = sender as RadioButton;
if (radioButton == null || !radioButton.Checked) return;
// your code ...

You can change the event handler of the Radiobutton inside your page events when page is posted back. For this, you need to use the Radiobutton value from VIEWSTATE and then assign event handler.

One generic work-around is a to use a listbox styled as radio buttons to prevent having to use multiple bindings/controls/checking on checked. There's only one control which event needs to be monitored ( SelectedIndexChanged ) and instead of checking which radiobutton is checked there's SelectedItem . Another advantage is the ability to bind datasources instead of dynamically adding radiobuttons.

For that purpose, here's a simple RadioButtonBox control that does just that (made it a long time ago and haven't revised it since, but still use it regularly)

public class RadioButtonBox:ListBox
{
    public readonly RadioButtonBoxPainter Painter;

    public RadioButtonBox()
    {
        Painter = new RadioButtonBoxPainter(this);
    }

    [DefaultValue(DrawMode.OwnerDrawFixed)]
    public override DrawMode DrawMode
    {
        get{return base.DrawMode;}
        set{base.DrawMode = value;}
    }
}

public class RadioButtonBoxPainter : IDisposable
{

    public readonly ListBox ListBox;
    public RadioButtonBoxPainter(ListBox ListBox)
    {
        this.ListBox = ListBox;
        ListBox.DrawMode = DrawMode.OwnerDrawFixed;
        ListBox.DrawItem += ListBox_DrawItem;
    }

    void ListBox_DrawItem(object sender, DrawItemEventArgs e)
    {
        if (e.Index == -1) return;
        Rectangle r = e.Bounds;
        r.Width = r.Height;
        bool selected = (e.State & DrawItemState.Selected) > 0;
        e.DrawBackground();
        e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
        ControlPaint.DrawRadioButton(e.Graphics, r, selected ? ButtonState.Checked : ButtonState.Normal);
        r.X = r.Right + 2;
        r.Width = e.Bounds.Width - r.X;
        string txt;
        if (ListBox.Site != null && ListBox.Site.DesignMode && e.Index >= ListBox.Items.Count)
            txt = ListBox.Name;
        else
            txt = ListBox.GetItemText(ListBox.Items[e.Index]);
        using (var b = new SolidBrush(e.ForeColor))
            e.Graphics.DrawString(txt, e.Font, b, r);
        if (selected)
        {
            r = e.Bounds;
            r.Width--; r.Height--;
            e.Graphics.DrawRectangle(Pens.DarkBlue, r);
        }
    }

    public void Dispose()
    {
        ListBox.DrawItem -= ListBox_DrawItem;
    }
}

The RadioButtonBox is the control, RadioButtonBoxPainter is the class that does the custom painting and can be used on any ListBox (the painting can be integrated into the control as well, but at the start this was made to give some existing listboxes a radiobutton look).

As for the display, normally it would still have that listbox feel:

单选按钮框

But by setting backcolor to 'Control' and no borderstyle, the result looks like regular radiobuttons:

RadioButtonBox2

If the second layout is always preferred, they can always be set as defaults in the RadioButtonBox control.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM