Form validating event
See rules() for more details about specifying validation rules.
Use error Class, error Element, wrapper, error Label Container, error Container, show Errors, success, error Placement, highlight, unhighlight, and ignore Title to control how invalid elements and error messages are displayed.
The following code example uses the derived class Text Box and validates an email address that the user enters. Length) ' Set the Error Provider error with the text to display. Set Error(text Box1, error Msg) End If End Sub Private Sub text Box1_Validated(By Val sender As Object, _ By Val e As System. Validated ' If all conditions have been met, clear the error provider of errors. Set Error(text Box1, "") End Sub When you change the focus by using the keyboard (TAB, SHIFT TAB, and so on), by calling the Select or Select Next Control methods, or by setting the Container Control.
If the email address is not in the standard format (containing "@" and "."), the validation fails, an Error Provider icon is displayed, and the event is canceled. Active Control property to the current form, focus events occur in the following order: Caution Do not attempt to set focus from within the Enter, Got Focus, Leave, Lost Focus, Validating, or Validated event handlers.
When an element is valid, the following things are true: Adds a custom error message to the element; if you set a custom error message, the element is considered to be invalid, and the specified error is displayed.
This lets you use Java Script code to establish a validation failure other than those offered by the standard constraint validation API.
Use submit Handler to implement your own form submit, eg. Use invalid Handler to react when an invalid form is submitted.
Use rules and messages to specify which elements to validate, and how.
If the entered data follows all of the specified rules, it is considered valid; if not, it is considered invalid. In that case you should also somehow tell the user that there’s a problem with the field. For example, if the user presses Tab, clicks on another control, presses Enter to trigger the Accept button, or presses Esc to trigger the Cancel button.For example, you could display an error message, change the field’s background color, or at least beep to let the user know that focus did not leave the field because something is wrong. The event triggers even if the user tries to close the form.That means the user cannot close the form unless the field passes validation.Note also that the user can leave a field if it doesn’t have a This entry was posted in controls and tagged C#, C# programming, controls, example, example program, validate field, validate form, Validating event, Windows Forms programming.