Question

Overview: I have a groupBox with 3 radio buttons in it. 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.

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

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:

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

Solution

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 -

private void optStatusClosed_CheckedChanged(object sender, EventArgs e)
{
    if((sender as RadioButton).Checked)
    {
       statusChanged();
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top