Radio button checked changed event fires twice

2020-02-05 11:47发布

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?

6条回答
Root(大扎)
2楼-- · 2020-02-05 12:27

To avoid it, just check if radioButton is checked

for example:

private void radioButton1_CheckedChanged(object sender, EventArgs e)
{
    if (radioButton1.Checked == true)
        //your code
}
查看更多
在下西门庆
3楼-- · 2020-02-05 12:43

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

查看更多
狗以群分
4楼-- · 2020-02-05 12:43

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;
    }
查看更多
对你真心纯属浪费
5楼-- · 2020-02-05 12:47
{
    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";
            }
        }

    }
}
查看更多
Animai°情兽
6楼-- · 2020-02-05 12:48

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)

查看更多
神经病院院长
7楼-- · 2020-02-05 12:51

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);
        }
    }
}
查看更多
登录 后发表回答