C# 单选按钮检查更改事件触发两次
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11493845/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Radio button checked changed event fires twice
提问by Muhammad Ali Dildar
Please read my question its not a duplicate one.
请阅读我的问题,它不是重复的问题。
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.
我在 Windows 窗体上有三个单选按钮,所有这些按钮都关联了常见的“CheckedChanged”事件。当我单击这些单选按钮中的任何一个时,它会触发 'CheckedChanged' 事件两次。
Here is my code:
这是我的代码:
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?
我插入了断点,此事件中的整个代码迭代了两次。请告诉我为什么它会这样?
采纳答案by David Hall
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 时,另一个将被取消选中 - 因此检查的更改事件将触发两次。
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:
要仅在此事件中为刚刚选择的 RadioButton 执行任何操作,您可以查看发送方对象,执行如下操作:
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);
}
}
}
回答by kmatyaszek
CheckedChanged is raised whenever the Checked property changes. If you select a RadioButton then the previously selected RadioButton is unchecked (fired CheckedChanged), and then the new RadioButton is checked (fired CheckedChanged).
每当 Checked 属性更改时,都会引发 CheckedChanged。如果您选择 RadioButton,则取消选中之前选择的 RadioButton(触发 CheckedChanged),然后选中新的 RadioButton(触发 CheckedChanged)。
回答by Rafael Dowling Goodman
It's triggering once for the radio button transition from checked to unchecked, and again for the radio button transitioning from unchecked to checked (i.e. any change in checked state triggers the event)
它触发一次单选按钮从选中状态转换为未选中状态,并再次触发单选按钮从未选中状态转换为选中状态(即选中状态的任何更改都会触发事件)
回答by Steve Stover
You could set the AutoCheck property true for each RadioButton then catch the Click event instead of the CheckChanged event. This would ensure that only one event is fired, and the logic in the handler can cast the sender to type RadioButton if needed to process the click. Often the cast can be avoided if the handler logic is simple. Here is an example which handles three controls, rbTextNumeric, rbTextFixed and rbTextFromFile:
您可以为每个 RadioButton 设置 AutoCheck 属性为 true,然后捕获 Click 事件而不是 CheckChanged 事件。这将确保只触发一个事件,如果需要处理点击,处理程序中的逻辑可以将发送者转换为类型 RadioButton。如果处理程序逻辑很简单,通常可以避免强制转换。这是一个处理三个控件的示例,rbTextNumeric、rbTextFixed 和 rbTextFromFile:
private void rbText_Click(object sender, EventArgs e)
{
flowLayoutPanelTextNumeric.Enabled = rbTextNumeric.Checked;
txtBoxTextFixed.Enabled = rbTextFixed.Checked;
flowLayoutPanelTextFromFile.Enabled = rbTextFromFile.Checked;
}
回答by Emin
{
public partial class Form3 : Form
{
public Form3()
{
InitializeComponent();
}
int click = 0;
private void radioButton1_Click(object sender, EventArgs e)
{
click++;
if (click %2==1)
{
radioButton1.Checked = true;
}
if (click %2==0)
{
radioButton1.Checked = false;
}
if (radioButton1.Checked==true)
{
label1.Text = "Cheked";
}
if (radioButton1.Checked==false)
{
label1.Text = "Uncheked";
}
}
}
}
回答by jaleel
To avoid it, just check if radioButtonis checked
为避免它,只需检查是否radioButton已选中
for example:
例如:
private void radioButton1_CheckedChanged(object sender, EventArgs e)
{
if (radioButton1.Checked == true)
//your code
}

