1

在我的项目中,有两个单选按钮。我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 中做错了什么。请帮忙。

4

4 回答 4

2

您可以检查发件人 RadioButton 并相应地放置您的代码,如下所示 -

    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.
        }
    }
于 2012-11-14T13:49:57.860 回答
1

对于一个非常简单(尽管很粗略)的解决方案是挂接两个单选按钮,而仅将其中一个挂接到处理程序:由于选中一个单选会取消选中另一个单选按钮,因此它将按预期工作。

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

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 中抽象一点。

于 2012-11-14T13:32:13.690 回答
-1

Put both radio buttons in the same panel or groupbox and automatically they will be grouped so that only one can be selected at a time.

于 2012-11-14T13:33:22.430 回答
-1

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

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

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
        }
于 2014-01-09T04:53:06.090 回答