Radio button checked changed event fires twice
Asked Answered
F

8

43

Please read my question its not a duplicate one.

I've three radio buttons on windows form and all these buttons have common 'CheckedChanged' event associated. When I click any of these radio buttons, it triggers the 'CheckedChanged' event twice.

Here is my code:

private void radioButtons_CheckedChanged(object sender, EventArgs e)
{
    //My Code
}

I inserted the breakpoint and the whole code within this event iterates twice. Please tell me why it is behaving like this?

Fictionalize answered 15/7, 2012 at 16:59 Comment(2)
your question is already answered please see post #8050796Cracker
It doesn't appear that your linked answer is related to this question.Gyrostat
B
85

As the other answerers rightly say, the event is fired twice because whenever one RadioButton within a group is checked another will be unchecked - therefore the checked changed event will fire twice.

To only do any work within this event for the RadioButton which has just been selected you can look at the sender object, doing something like this:

void radioButtons_CheckedChanged(object sender, EventArgs e)
{
    RadioButton rb = sender as RadioButton;
    if (rb != null)
    {
        if (rb.Checked)
        {
            // Only one radio button will be checked
            Console.WriteLine("Changed: " + rb.Name);
        }
    }
}
Basket answered 15/7, 2012 at 17:20 Comment(2)
Don't you thin 'if (rb != null)' is unnecessary?Fictionalize
@MuhammadAliDildar It avoids any chance of a null reference exception here - of course since you wire up all the events, you can be pretty sure that this event handler will only be attached to a radiobutton, but the 'best practice' way of doing things is to check that the cast worked. Then again - from a certain point of view throwing an exception when you attach the event handler incorrectly may be preferable so you can fix the code.Basket
V
6

To avoid it, just check if radioButton is checked

for example:

private void radioButton1_CheckedChanged(object sender, EventArgs e)
{
    if (radioButton1.Checked)
        //your code
}
Vanpelt answered 9/1, 2017 at 10:16 Comment(1)
better to use the sender as RadioButton like in David's answerEnunciation
L
5

CheckedChanged is raised whenever the Checked property changes. If you select a RadioButton then the previously selected RadioButton is unchecked (fired CheckedChanged), and then the new RadioButton is checked (fired CheckedChanged).

Leonleona answered 15/7, 2012 at 17:5 Comment(0)
W
2

It's triggering once for the radio button transition from checked to unchecked, and again for the radio button transitioning from unchecked to checked (i.e. any change in checked state triggers the event)

Ware answered 15/7, 2012 at 17:7 Comment(0)
W
1

The other answers are correct but miss the reason for the underlying problem. When a radio button is checked the first event sent is the change from the unchecked item however if you check its state by its control name you will still see its old checked status because the form has not been updated yet. To see its true status you need to cast the sender object. This allows you to perform any actions relating to the condition which is being deselected should you need to do so.

In the not uncommon scenario below multiple radio buttons are sent to the same handler event. Simply checking the state of the sender for checked will not work here as we need to perform different actions depending on which radio button has been pressed. So first we ignore any sender that has just been unchecked. then we identify the checked sender by control name to process the correct action.

private void ModeChangedExample(object sender, EventArgs e)
{
    // multiple radio buttons come here
    // We only want to process the checked item.
    // if you need to something based on the item which was just unchecked don't use this technique. 
    // The state of the sender has not been updated yet in the form.
    // so checking against rdo_A check state will still show it as checked even if it has just been unchecked
    // only the sender variable is up to date at this point.
        
    // To prevent processing the item which has just been uncheked
    RadioButton RD = sender as RadioButton;
    if (RD.Checked == false) return;

    if (rdo_A.Name == RD.Name)
    {
        //Do stuff
    }

    if (rdo_B..Name == RD.Name)
    {
        // Do other stuff
    }

    if (rdo_C.Name == RD.Name)
    {
        // Do something else
    }

}
Warehouse answered 5/8, 2020 at 11:32 Comment(0)
Z
0

You could set the AutoCheck property true for each RadioButton then catch the Click event instead of the CheckChanged event. This would ensure that only one event is fired, and the logic in the handler can cast the sender to type RadioButton if needed to process the click. Often the cast can be avoided if the handler logic is simple. Here is an example which handles three controls, rbTextNumeric, rbTextFixed and rbTextFromFile:

        private void rbText_Click(object sender, EventArgs e)
    {
       flowLayoutPanelTextNumeric.Enabled = rbTextNumeric.Checked;
       txtBoxTextFixed.Enabled = rbTextFixed.Checked;
       flowLayoutPanelTextFromFile.Enabled = rbTextFromFile.Checked;
    }
Zebada answered 29/4, 2013 at 2:39 Comment(1)
This appears to be true for Windows Forms but not for WebForms.Bethink
J
0
{
    public partial class Form3 : Form
    {
        public Form3()
        {
            InitializeComponent();
        }
        int click = 0;
        private void radioButton1_Click(object sender, EventArgs e)
        {
            click++;
            if (click %2==1)
            {
                radioButton1.Checked = true;
            }
            if (click %2==0)
            {
                radioButton1.Checked = false;
            }
            if (radioButton1.Checked==true)
            {
                label1.Text = "Cheked";
            }
            if (radioButton1.Checked==false)
            {
                label1.Text = "Uncheked";
            }
        }

    }
}
Justinejustinian answered 29/1, 2014 at 21:43 Comment(1)
@davidkonrad real code should never need any explanation! Doh!Diatom
W
0

This problem of double checking happens when there is a series of RadioButton Clicks in succession.I had this same problem.The last click will give two results.To overcome this i made a dummy click in the end.The double click stopped.Try this method. Venkatraman

Walkling answered 8/6, 2021 at 13:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.