Controlling Validation Behavior with the Auto Validate Property

By default, Windows Forms 2.0 applications will behave just like previous versions if you set the Cancel property of the Validating event's CancelEventArgs argument to TRuethis prevents the focus from leaving that control and terminates the validation process, s<Validated won't fire. This is done for backward-compatibility reasons.

However, a new property has been introduced to the ContainerControl class that will let you modify this behavior if desired. If you plan to perform validation programmatically for the entire form, or just don't want to force users to correct problems one control at a time, you can change the value of the AutoValidate property for the form. This property takes a value of the^utoValidate enumeration type, which can be set to any of the values described in Table 10.3. The default value for this property is EnablePreventFocusChange, which models the .NET 1.1 behavior.

Table 10.3. AutoValidate Enumeration Values




Disables automatic validation. When you set the Cancel property of the Validating event argument to true, it will still keep the Validated event from firing, but users can still change focus and close the form.


If Cancel is set to true, focus will remain on the control that failed validation and th^/alidated event won't fire. Users won't be able to close the form until validating is successful.


If Cancel is set to true, focus is allowed to change to other controls on the form, but theValidated event won't fire, and users won't be able to close the form until validation is successful.


The control's behavior is determined by the AutoValidate setting of its parent. If there is no parent control, then the behavior is the same as if EnablePreventFocusChange was set.

Typically, you will set AutoValidate equal to EnableAllowFocusChange or Disable if you plan to perform manual validation at a form level using the ValidateChildren method, which is described in the next section.


+1 0


  • sebastian
    What is autovalidate property in vb. net?
    2 years ago

Post a comment