Search code examples
c#textboxmessagebox

Display box showing twice C#


I am performing a validation for my ID field when the user chooses what format it will be. The validation works except for the fact that when it displays the messagebox you select OK, then it repeats once more before going back to the form. I have supplied my code for it below. I used the dialogresult part to see if making it perform an action would stop the problem. This was fixed by using the Click event as mentioned by one of the answers.

private void rChkBoxB_ToggleStateChanged(object sender, Telerik.WinControls.UI.StateChangedEventArgs args)
{
    if (Convert.ToInt32(rTxtBoxFormatID.Text) > 256)
    {
        DialogResult dialogresult = MessageBox.Show("B does not support numbering over a number!", "Error", 
            MessageBoxButtons.OK, MessageBoxIcon.Error);
        if (dialogresult == DialogResult.OK)
        {
            rChkBoxB.Checked = false;
        }
    }
}

The new problem is turning the checkbox back to false after the click event. Here is my updated code. The typical checkbox.checked = false does not change it back to false. I used breakpoint to verify I am getting the to the if statement and the value says false for my checkbox. How can I change the property back to false?

private void rChkBoxB_Click(object sender, EventArgs e)
{
    if (Convert.ToInt32(rTxtBoxFormatID.Text) > 256)
    {
        DialogResult dialogresult = MessageBox.Show("B does not support numbering over a number!", "Error",
            MessageBoxButtons.OK, MessageBoxIcon.Error);
        if (dialogresult == DialogResult.OK)
        {
            rChkBoxB.Checked = false;
        }
    }
}

Solution

  • Because you've set Checked = false in return of the dialogresult? Hence, toggling the state again?

    EDIT: Of note, you are probably using the CheckStateChanged event. So, you check the checkbox (setting it to true), it fires, you show the MessageBox, you set the checkbox back to false, which in turn, causes the event to fire again (you are changing the state).

    This is where you could use the Click event, as opposed to setting a flag (proposed solution). That way when you set the checkbox back to false, in code, the Click event won't fire. At runtime, a Click event, for a checkbox, will always toggle the checkbox on/off, no matter where you click on the checkbox.

    In conclusion, just use the Click event:

        private void rChkBoxB_Click(object sender, EventArgs e)
        {
            if (Convert.ToInt32(rTxtBoxFormatID.Text) > 256)
            {
                DialogResult dialogresult = MessageBox.Show("B does not support numbering over a number!", "Error",
                    MessageBoxButtons.OK, MessageBoxIcon.Error);
                if (dialogresult == DialogResult.OK)
                {
                    rChkBoxB.Checked = false;
                }
            }
        }
    

    Of course, in either version of your code, I'm assuming you want to actually check if Text > 256 when you are checking it to true. So you might want to:

        private void rChkBoxB_Click(object sender, EventArgs e)
        {
            if (rChkBoxB.Checked == false) return; // NEW CODE HERE
    
            if (Convert.ToInt32(rTxtBoxFormatID.Text) > 256)
            {
                DialogResult dialogresult = MessageBox.Show("B does not support numbering over a number!", "Error",
                    MessageBoxButtons.OK, MessageBoxIcon.Error);
                if (dialogresult == DialogResult.OK)
                {
                    rChkBoxB.Checked = false;
                }
            }
        }