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

7
  • 3
    No, but why not just to process this in code? I mean check that button is checked in CheckedChanged event? Commented Jul 15, 2015 at 12:36
  • you might be interested in inspecting the sender during that event. Commented Jul 15, 2015 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. Commented Jul 15, 2015 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. Commented Jul 15, 2015 at 12:50
  • if you want to post this as answer, I'd mark it. It prevent unnecessary event calls Commented Jul 15, 2015 at 13:28

4 Answers 4

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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.
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 ... 

Comments

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.

Comments

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.