UserControl Validating function is called even when AutoValidate is set to Disable on both the UC's design and the UC's instance

Viewed 62
  1. Created a UserControl with many data bound child controls on it, none of which have Validating events.
  2. Gave the root of the UC a Validating event.
  3. Set the root of the UC to AutoValidate:Disabled
  4. Placed the UC on a main form.
  5. Set the UC instance's properties on the main form to AutoValidate:Disabled
  6. Main form has Edit, Save, Cancel buttons. Only Save calls uc.ValidateChildren().

Expected result: Validation function only called when I click Save on main form.

Actual result: Validation function is also called when I click Cancel on main form. I greatly desire that it not be called, because this is the whole point of a Cancel button.

2 Answers

The main question is: do you want automatic validation?

◾ If you don't want automatic validation at form level:

  • Set AutoValidate property of the Form to Disable and no Validating event will raise when you move between controls.
  • Call ValidateChildren when you want to trigger validating events.

◾ If you want automatic validation at form level, but you don't want validation when clicking X button or a Cancel button:

In my case the fix was:

  1. Set the root of the main form to AutoValidate:Disabled. Merely setting this on the instance of the UC was not sufficient.
Related