单选按钮检查变更事件触发两次 [英] Radio button checked changed event fires twice
问题描述
请阅读我的问题,它不是一个重复的。
Please read my question its not a duplicate one.
我已经在Windows上形成了三个单选按钮,所有这些按钮有共同的CheckedChanged事件相关联。当我点击这些单选按钮时,会触发的CheckedChanged事件两次
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.
下面是我的代码:
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?
推荐答案
至于其他的回答者正确地说,在事件被触发两次,因为每当出现一个单选按钮组中被选中另一个将被取消选中 - 因此检查更改事件将触发两次
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);
}
}
}
这篇关于单选按钮检查变更事件触发两次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!