The bad news is that you have to script your validation and it can change depending on the data type and format. Validation Example 1: Checking if a Text Box Field is Empty For the first example, we will create a simple function that will ensure the field is empty as well as making sure it doesn’t only contain blank spaces.

In this example, we cancel the validation, when the field is empty or has only whitespaces.

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.

Use this event to provide custom validation for any input control, such as a Text Box. In other words, the validating control will retain the focus until the user provides the correct format, even if the user clicks another control such as a button or textbox.

I.e., the caret will remain in the textbox until the user enters the correct format.

Typically you will be validating text that is provided by a Text Box, but it could also come from a Combo Box where the user can type their selection.

As for validating the text, it can range from simply ensuring there is value (i.e., not an empty string) or more complex task such as validating the format of an email address. Once you have written a validation function for a specific format, convert it into a snippet, so the next time you need to validate similar data, you will save time by simply inserting your snippet.

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.

Let’s update Example 1 to display an Error Message using an Error Provider Control: Next add a Validated event to clear in the Error Provider’s message.

Since our event will only clear the error message, it can be used as a generic event shared by all validating controls.

A good user interface will validate user input to ensure it is in the correct format.