Radio button checked changed event fires twice

后端 未结 7 1002
梦谈多话
梦谈多话 2020-12-03 16:52

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

相关标签:
7条回答
  • 2020-12-03 17:31

    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);
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题