1

私のプロジェクトでは、2 つの radioButtons があります。CheckedChanged私は次のようなことをして同じイベントを与えました
:

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

両方の RadioButton を に保持して、Panel一方を true にし、もう一方を false にしました。
問題は、RadioButton_CheckedChangedイベントで非常に大きなコードを実装していることです。
ユーザーが 2 つの RadioButton のいずれかの状態を変更するたびに、イベントが 2 回発生します。
何時間も答えが得られた後、RadioButton の両方の状態が変更されているため、イベントが 2 回発生しています (したがって、イベントは 2 回発生します)。この問題を解決するために、次のようなイベントを一時的にフック解除しようとしています:

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

イベントは2回実行されていますが。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.
        }
    }

アップデート

両方の radioButton に 2 つの異なるハンドラーを使用できず、チェックボックスがオンになっている場合にのみコードを実行したい場合は、これを行うことができます -

    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

非常に単純な (大雑把ではありますが) 解決策は、両方のラジオ ボタンをフックせ、そのうちの 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

その遅い解決策ですが、あなたの質問に対する正しい答えがないことがわかったので、あなたにとってうまくいくかもしれないと投稿しています

ラジオボタンの両方のクリックイベントを作成し、ラジオボタンがチェックされてコードが実行されるたびにコードを単純に配置します:):):)

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 に答える
-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 に答える