1

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)?

Community
  • 1
  • 1
Cher
  • 2,789
  • 10
  • 37
  • 64
  • 3
    No, but why not just to process this in code? I mean check that button is checked in CheckedChanged event? – demonplus Jul 15 '15 at 12:36
  • you might be interested in inspecting the `sender` during that event. – Amit Kumar Ghosh Jul 15 '15 at 12:38
  • 2
    I know I can do this in code, but I just wanted to make sure there was no direct event that was more appropriate instead of using one which is called when not needed. – Cher Jul 15 '15 at 12:44
  • 1
    Don't know if you would be interested, but to avoid these scenario's I use a radiobuttonbox (basically a listbox painted with radio buttons). That way I only have to listen to SelectedIndexChanged (and get the SelectedItem). In the end it's just another workaround, but it saves me from having to do the same check over again. – Me.Name Jul 15 '15 at 12:50
  • if you want to post this as answer, I'd mark it. It prevent unnecessary event calls – Cher Jul 15 '15 at 13:28
  • Allrighty, will do, hadn't seen your response yet (best to use @username to notify someone ;) ) – Me.Name Jul 16 '15 at 07:23
  • @Me.Name, creative solution that nicely meets requirements; I will be using your approach for a dynamic data entry form app. – Developer63 Mar 07 '16 at 20:28

4 Answers4

2

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 ...
sara
  • 3,521
  • 14
  • 34
2

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.

Mallikarjuna
  • 130
  • 9
2

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:

RadioButtonBox

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.

Me.Name
  • 12,259
  • 3
  • 31
  • 48
  • Great solution that exactly addresses the criteria, only one control to monitor, and only one event to handle. Plus, as mentioned, has the bonus of being able to bind a datasource to provide the list of buttons. Plus, it's easier to work just one control at at design time instead of fiddling with multiple radio buttons and (typically) a groupbox. – Developer63 Mar 07 '16 at 20:17
0

I had the same problem, managed to fix it by placing all the radio buttons in a group box, and make a singular event for all the buttons and adding:

if (Test.Checked == true)
{
}

This way it will only fire for the one that is getting checked, not the ones that get unchecked.

A.bakker
  • 221
  • 1
  • 9