locked
ErrorProvider disables all other buttons/events on windows form RRS feed

  • Question

  • I've got a textbox on a form I'd like to be validated before entering the data in the database.


    private void textboxPrice_Validating(object sender, CancelEventArgs e)
     {
      TextBox tb = (TextBox)sender;
      string errorMsg;
      if (!_v.CheckValuta(tb.Text, out errorMsg))
      {
      e.Cancel = true;
      error.SetError(tb, errorMsg);
      }
     }
    
    When the validating is done I unset the errorProvider.
     private void InputValidated(object sender, EventArgs e)
     {
      TextBox tb = (TextBox)sender;
      error.SetError(tb, "");
     }
    

    If there is an errorProvider still active I can't close the form or go to another textbox or Control
     private void ButtonCancel_Click(object sender, EventArgs e)
     {
      Close();
     }
    
    I can close the form when I open it and directly close it. But when I enter a control that needs to be validated, I'm not able to leave it until it has been validated. So I can't close the form or go to other controls.

    How can I close the form without validating all textboxes?

    BTW, this form works perfectly fine when all values are entered correctly.

     

    private void ButtonOK_Click(object sender, EventArgs e)
     {
      Boolean invalidInput = false;
      foreach (Control ctrl in this.Controls)
      {
      if (error.GetError(ctrl) != "")
      {
       invalidInput = true;
       break;
      }
      }
      if (!invalidInput)
      {
      //Insert/Update data into the database (using LINQ)
      }
     }
    
    

     

    Saturday, June 12, 2010 8:45 AM

Answers

  • Hi,

    Put the following code in your form and try now.

    private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
          e.Cancel = false; 
        }

     

    • Marked as answer by KwintenB Saturday, June 12, 2010 8:49 PM
    Saturday, June 12, 2010 11:52 AM
  • Hi,

    Set the CausesValidation Property of the Button control to false so that you can bypass the ErrorProvider validation for that control.

    • Marked as answer by KwintenB Saturday, June 12, 2010 8:49 PM
    Saturday, June 12, 2010 4:24 PM

All replies

  • Hi,

    Put the following code in your form and try now.

    private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
          e.Cancel = false; 
        }

     

    • Marked as answer by KwintenB Saturday, June 12, 2010 8:49 PM
    Saturday, June 12, 2010 11:52 AM
  • That works when I close the form using the red cross or alt+f4.

    But all the other controls (buttons, textboxes, comboboxes,...) are still disabled.

    Saturday, June 12, 2010 1:15 PM
  • Hi,

    Set the CausesValidation Property of the Button control to false so that you can bypass the ErrorProvider validation for that control.

    • Marked as answer by KwintenB Saturday, June 12, 2010 8:49 PM
    Saturday, June 12, 2010 4:24 PM