单选按钮事件执行两次



在我的项目中,有两个单选按钮。我已经通过做
同样的事情CheckedChanged事件像这样:

DoctorRadioButton.CheckedChanged += new EventHandler(RadioButton_CheckedChanged);
PatientRadioButton.CheckedChanged += new EventHandler(RadioButton_CheckedChanged);

我将两个单选按钮都保留在一个Panel中,使它们一个为真,而另一个为假。
现在的问题是我在RadioButton_CheckedChanged事件中实现了非常大的代码。
每当用户更改两个单选按钮中的任何一个的状态时,事件都会引发两次。
经过这么多小时,我得到了答案,该事件正在引发两次,因为两个单选按钮状态都被更改了(因此,该事件将被引发两次)。为了解决这个问题,我正在尝试暂时解开事件,如下所示:

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);
     }

即使事件正在执行两次。我知道我在钩子和解钩上做错了什么。请帮忙。

您可以检查发送方单选按钮并相应地放置代码,如下所示 -

    void RadioButton_CheckedChanged(object sender, EventArgs e)
    {
        RadioButton senderRadioButton = sender as RadioButton;
        if (senderRadioButton.Equals(DoctorRadioButton))
        // OR senderRadioButton.Name == "DoctorRadioButton"
        {
            // Place your code here for DoctorRadioButton.
        }
        else
        {
            // Place your code here for PatientRadioButton.
        }
    }

更新

如果您不能对两个单选按钮使用两个不同的处理程序,并且只想在选中复选框的情况下执行代码,则可以这样做 -

    void RadioButton_CheckedChanged(object sender, EventArgs e)
    {
        RadioButton senderRadioButton = sender as RadioButton;
        if (senderRadioButton.IsChecked)
        {
            // Place your code here for check event.
        }
    }

对于一个非常简单(尽管粗糙)的解决方案是不挂钩两个单选按钮,而只将其中一个挂在处理程序上:由于选中一个无线电取消选中另一个无线电,它将按预期工作。

更复杂的方法是使用支持属性,如下所示:

class myForm
{
    private bool radioStatus = false; // depends on the default status of the radios
    private bool RadioStatus 
    {
        get{return radioStatus;} set {radioStatus = value; Checked_Changed();}
    }
    public myForm()
    {
    // Lambdas as handlers to keep code short.
    DoctorRadioButton.CheckedChanged += (s,args)=> 
        { if((s as RadioButton).Checked) RadioStatus = true; };
    PatientRadioButton.CheckedChanged += (s,args)=> 
        { if((s as RadioButton).Checked) RadioStatus = false; };
    }
    void Checked_Changed()
    {
        if (RadioStatus) // = true --> DoctorRadioButton was checked
        {
            //code
        }
        else // = false --> PatientRadioButton was checked
        {
            //other code
        }
    }
}

此方法的优点是允许您从 UI 中抽象出一点。

将两个单选按钮放在同一个面板或分组框中,它们将自动分组,以便一次只能选择一个。

这是一个迟到的解决方案,但我发现你的问题没有正确的答案,所以我正在发布它可能对你有用

为单选按钮

和简单放置代码创建单击事件,因为每次单击时都会选中单选按钮并且代码执行:):):)

private void DoctorRadioButtons_Click(object sender, EventArgs e)
        {
           //Your code on Doctor Radio Button
        }
private void PatientRadioButton_Click(object sender, EventArgs e)
        {
           // Your code on Patient Radio Button
        }

最新更新