问题描述
请阅读我的问题,它不是重复的.
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?
推荐答案
作为另一个答案正确说明,事件被解雇了两次,因为每当一个组内的一个radiobutton被检查另一个,都将取消选中 - 因此,被检查的更改的事件将触发两次.
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.
要仅在此事件中为刚刚被选中的 RadioButton 执行任何工作,您可以查看 sender 对象,执行以下操作:
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);
}
}
}
这篇关于选中的单选按钮已更改事件触发两次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!