0

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

问题: 它当前的工作方式我每次更改都会弹出两次消息,因为它正在为选中的事件注册已更改的事件 = true 并再次在已选中的事件上注册 = false 用于曾经被选中的事件。

我会假设有更好的方法来做到这一点,但我不确定从 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();
}
4

1 回答 1

2

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

private void optStatusClosed_CheckedChanged(object sender, EventArgs e)
{
    if((sender as RadioButton).Checked)
    {
       statusChanged();
    }
}
于 2013-09-18T18:01:12.490 回答