Frage

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;
        }
    }
}
War es hilfreich?

Lösung

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;
            }
        }
    }

Andere Tipps

The code is not the problem, but the event ToggleStateChanged being called more than one time when rChkBoxB.Checked = false;

It run two times because you change the checked property inside the radio button. When you change it the CIL see that it generate a new flag and it does again the event.

For anticipating this you can put a second control in the first if

if (Convert.ToInt32(rTxtBoxFormatID.Text) > 256 && rChkBoxB.Checked)
{
  ///TODO
}

I also suggest to check if the input is correct: in this case you want only int so you can use int.TryParse(string, out int) (see reference here) or you can use Regex (references here)

Problem: ToggleStateChanged event will be fired whenever Checkbox state is being changed. Your checkbox state is getting changed when you set rChkBoxB.Checked = false; that is the reason why your ToggleStateChanged event is invoking twice.

Solution: Take a bool variable and make it true when user selects ok , then even if the ToggleStateChanged event triggers it wont invoke the function twice as isValid is set to true.

bool isValid=false;//declare as class variable

private void rChkBoxB_ToggleStateChanged(object sender, Telerik.WinControls.UI.StateChangedEventArgs args)
{
   if(!isValid)
   {
      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;
            isValid=true;
         }
         else
         {
            isValid=false;
         }
      }
   }
   else
   {
      isValid=false;
   }
}
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top