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


- 输入按键事件处理程序 2022-01-01
- Web Api 中的 Swagger .netcore 3.1,使用 swagger UI 设置日期时间格式 2022-01-01
- 如何用自己压缩一个 IEnumerable 2022-01-01
- C#MongoDB使用Builders查找派生对象 2022-09-04
- WebMatrix WebSecurity PasswordSalt 2022-01-01
- 带有服务/守护程序应用程序的 Microsoft Graph CSharp SDK 和 OneDrive for Business - 配额方面返回 null 2022-01-01
- 在哪里可以找到使用中的C#/XML文档注释的好例子? 2022-01-01
- MoreLinq maxBy vs LINQ max + where 2022-01-01
- 良好实践:如何重用 .csproj 和 .sln 文件来为 CI 创建 2022-01-01
- C# 中多线程网络服务器的模式 2022-01-01