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?
推荐答案
作为另一个答案正确说明,事件被解雇了两次,因为每当一个组内的一个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);
}
}
}
这篇关于选中的单选按钮已更改事件触发两次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:选中的单选按钮已更改事件触发两次


基础教程推荐
- 经典 Asp 中的 ResolveUrl/Url.Content 等效项 2022-01-01
- 在 VS2010 中的 Post Build 事件中将 bin 文件复制到物 2022-01-01
- 首先创建代码,多对多,关联表中的附加字段 2022-01-01
- 错误“此流不支持搜索操作"在 C# 中 2022-01-01
- 是否可以在 asp classic 和 asp.net 之间共享会话状态 2022-01-01
- 如何动态获取文本框中datagridview列的总和 2022-01-01
- JSON.NET 中基于属性的类型解析 2022-01-01
- 从 VS 2017 .NET Core 项目的发布目录中排除文件 2022-01-01
- 将事件 TextChanged 分配给表单中的所有文本框 2022-01-01
- 全局 ASAX - 获取服务器名称 2022-01-01