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


基础教程推荐
- 将 Office 安装到 Windows 容器 (servercore:ltsc2019) 失败,错误代码为 17002 2022-01-01
- 如何在 IDE 中获取 Xamarin Studio C# 输出? 2022-01-01
- SSE 浮点算术是否可重现? 2022-01-01
- 为什么Flurl.Http DownloadFileAsync/Http客户端GetAsync需要 2022-09-30
- 将 XML 转换为通用列表 2022-01-01
- c# Math.Sqrt 实现 2022-01-01
- rabbitmq 的 REST API 2022-01-01
- 如何激活MC67中的红灯 2022-01-01
- MS Visual Studio .NET 的替代品 2022-01-01
- 有没有办法忽略 2GB 文件上传的 maxRequestLength 限制? 2022-01-01