Validation with GotFocus and LostFocus

The best time to validate the a field’s contents is when the user attempts to leave the field. The validate event apply to when user attempt to set focus to another field on the same form or  when the form unloads from memory while the current field has focus.

You should take the following steps to implement field-level validation
with the Validate event:

  1. Determine which controls (such as CancelButtons) should not trigger the Validate event when the user tries to set focus to  them and set the CausesValidation property of these controls to False (default is True).
  2. Write validation code (or call your own validation routines) in the Validate event procedure of all controls where you need to have validation in place.
  3. If your validation code decides that the control’s data is not valid, you can either:
  •  Fix the problem right there in the validation code.
  • Force the focus to remain in the current control (presumably so that the user can rectify the problem) by setting the value of the Validate event’s Cancel parameter to True.

Leave a Reply