在我的项目中,有两个单选按钮。我CheckedChanged
通过这样做给了同样的事件
:
DoctorRadioButton.CheckedChanged += new EventHandler(RadioButton_CheckedChanged);
PatientRadioButton.CheckedChanged += new EventHandler(RadioButton_CheckedChanged);
我将两个 RadioButtons 都保留在 aPanel
中,以使它们为真,而另一个为假。
现在的问题是我在RadioButton_CheckedChanged
事件中实现了一个非常大的代码。
每当用户更改两个 RadioButtons 中的任何一个的状态时,该事件都会引发两次。
经过这么多小时我得到了答案,事件被引发了两次,因为两个 RadioButton 状态都被改变了(因此,事件将被引发两次)。为了解决这个问题,我试图暂时解除事件,如下所示:
RadioButton_CheckedChanged事件:(不工作)
if (DoctorRadioButton.Checked)
{
PatientRadioButton.CheckedChanged -= RadioButton_CheckedChanged; //Un
//
//My functions
//
PatientRadioButton.CheckedChanged += new EventHandler(RadioButton_CheckedChanged);
}
else
{
DoctorRadioButton.CheckedChanged -= RadioButton_CheckedChanged;
//
//My functions
//
DoctorRadioButton.CheckedChanged += new EventHandler(RadioButton_CheckedChanged);
}
即使事件执行了两次。我知道我在 Hooking 和 Unhooking 中做错了什么。请帮忙。