简体   繁体   English

radioButton.Checked_Changed事件触发次数过多

[英]radioButton.Checked_Changed event firing too many times

Overview: I have a groupBox with 3 radio buttons in it. 概述:我有一个包含3个单选按钮的groupBox。 I have code that is supposed to happen upon changing the radio button selected... for instance a message box asking "are you sure" only a bit more specific to each radio button. 我有更改所选单选按钮时应该发生的代码...例如,一个消息框询问“你确定”只对每个单选按钮更具体一些。 choices are Open, Close, and ReOpen. 选项包括打开,关闭和重新打开。 message boxes pop upon clicking close and reopen asking are you sure you want to Close and are you sure you want to reopen. 单击关闭后弹出消息框并重新打开询问您确定要关闭并确定要重新打开。

Problem: The way it is currently working I am getting the message popped up twice for each change because it is registering the changed event for checked = true on the one selected and again on the checked = false for the one that used to be selected. 问题:它当前正在工作的方式我正在为每次更改弹出两次消息,因为它在所选的一个上注册了checked = true的已更改事件,而对于曾经选择的那个,在checked = false上再次注册。

I would assume there is a better way to do this but I am not sure which event to use from the groupBox so that there is only one change event and not 2. here is a sample of the code I am using 我认为有一个更好的方法来做到这一点,但我不确定从groupBox使用哪个事件,以便只有一个更改事件,而不是2.这里是我正在使用的代码的示例

private void statusChanged()
{
    DialogResult choice;
    if (formLoading == false)
    {
        btnSave.Enabled = true;
        if (optStatusOpen.Checked == true)
        {
            optStatusOpen.Text = "Opened";
            optStatusClosed.Text = "Close";
            optStatusReOpened.Text = "Re-Open";
            optStatusClosed.Checked = false;
            optStatusClosed.Enabled = true;
            optStatusReOpened.Checked = false;
            optStatusReOpened.Enabled = false;
        }
        else if (optStatusClosed.Checked == true)
        {
            choice = MessageBox.Show("You've selected to close claim: " + txtCaimNumber.Text.ToUpper() + ". Continue?",
                                     "Close Claim Warning", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (choice == DialogResult.Yes)
            {
                if (closeClaim(txtClaimNumber.Text.ToUpper()) == true)
                {
                    MessageBox.Show("Claim successfully closed");
                    txtClaimNumber.Enabled = true;
                    optStatusClosed.Text = "Closed";
                    optStatusOpen.Text = "Open";
                    optStatusOpen.Checked = false;
                    optStatusOpen.Enabled = false;
                    optStatusReOpened.Text = "Re-Open";
                    optStatusReOpened.Checked = false;
                    optStatusReOpened.Enabled = true;
                    dtpCloseDate.Enabled = true;
                    txtClaimNumber.Focus();
                }
            }
            else//choice == no
            {
                formLoading = true;
                if (curStatus == 0) //open
                {
                    optStatusOpen.Checked = true;
                    optStatusClosed.Checked = false;
                    optStatusReOpened.Checked = false;
                }
                else if (curStatus == 2)//reopened
                {
                    optStatusOpen.Checked = false;
                    optStatusClosed.Checked = false;
                    optStatusReOpened.Checked = true;
                    dtpReopenDate.Enabled = true;
                }
                else // curStatus = 1 
                {
                    optStatusOpen.Checked = false;
                    optStatusClosed.Checked = true;
                    optStatusReOpened.Checked = false;
                }
            }
        }
        else //ReOpened.checked = true
        {
            curStatus = 3;
            choice = MessageBox.Show("You've selected to reopen claim: " + txtClaimNumber.Text.ToUpper() + ". Continue?",
                                     "Close Claim Warning", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (choice == DialogResult.Yes)
            {
                if (reOpenClaim(txtClaimNumber.Text.ToUpper()) == true)
                {
                    MessageBox.Show("Claim successfully ReOpened");
                    txtClaimNumber.Enabled = true;
                    optStatusOpen.Text = "Open";
                    optStatusReOpened.Text = "Re-Opened";
                    optStatusClosed.Text = "Close";
                    optStatusClosed.Enabled = true;
                    optStatusOpen.Checked = false;
                    optStatusOpen.Enabled = false;
                    txtClaimNumber.Focus();
                    dtpReopenDate.Enabled = true;
                    dtpReopenDate.Checked = true;                        
                }
            }
            else//choice == no
            {
                formLoading = true;
                optStatusOpen.Checked = false;
                optStatusClosed.Checked = true;
                optStatusReOpened.Checked = false;
            }
        }
    }
}

This method is sitting in the Checked_Changed event for each of the radio buttons like this: 对于每个单选按钮,此方法都位于Checked_Changed事件中,如下所示:

private void optStatusClosed_CheckedChanged(object sender, EventArgs e)
{
    if ((sender as RadioButton).Checked == true) //added this line to solve the problem
        statusChanged();
}

If you want the statusChanged() to be called only for radio button which is checked right now, you can check IsChecked property of radio button in your handler - 如果你想只为现在选中的单选按钮调用statusChanged() ,你可以在你的处理程序中检查单选按钮的IsChecked属性 -

private void optStatusClosed_CheckedChanged(object sender, EventArgs e)
{
    if((sender as RadioButton).Checked)
    {
       statusChanged();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM