收音机Button.CheckedChanged事件发射次数太多

时间:2013-09-18 17:55:47

标签: c# events radio-button groupbox

概述:我有一个包含3个单选按钮的groupBox。我有更改所选单选按钮时应该发生的代码...例如,一个消息框询问“你确定”只对每个单选按钮更具体一些。选项包括打开,关闭和重新打开。单击关闭后弹出消息框并重新打开询问您确定要关闭并且确定要重新打开。

问题:当前正在工作的方式我每次更改都会弹出两次消息,因为它在所选的一个上注册了checked = true的已更改事件,并且再次检查了=对于曾经被选中的人来说是假的。

我认为有更好的方法可以做到这一点,但我不确定在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;
            }
        }
    }
}

此方法位于每个单选按钮的Checked_Changed事件中,如下所示:

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

1 个答案:

答案 0 :(得分:2)

如果您希望仅为现在选中的单选按钮调用statusChanged(),则可以在处理程序中检查单选按钮的IsChecked属性 -

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