The Format event on a data binding can be used to reformat incoming data to comply with the mask, and the Parse event can be used to reformat outgoing data to comply with the specifications of the data field.
For more information, see Masked Text Box Control (Windows Forms).
If you want full programmatic control over validation, or need to perform complex validation checks, you should use the validation events built into most Windows Forms controls.
In the Validating event-handling method, you can validate user input in several ways.
For example, if you have a text box that must contain a postal code, you can perform the validation in the following ways: Validation is very useful when you have bound your controls to a data source, such as a database table.
By using validation, you can make sure that your control's data satisfies the format required by the data source, and that it does not contain any special characters such as quotation marks and back slashes that might be unsafe.
When users enter data into your application, you may want to verify that the data is valid before your application uses it.
You may require that certain text fields not be zero-length, that a field be formatted as a telephone number or other type of well-formed data, or that a string not contain any unsafe characters that could be used to compromise the security of a database.
Windows Forms provides several ways for you to validate input in your application.
If you need to require users to enter data in a well-defined format, such as a telephone number or a part number, you can accomplish this quickly and with minimal code by using the Masked Text Box control.
A is a string made up of characters from a masking language that specifies which characters can be entered at any given position in the text box. If the user types an incorrect entry, for example, the user types a letter when a digit is required, the control will automatically reject the input.
The masking language that is used by Masked Text Box is very flexible.
It allows you to specify required characters, optional characters, literal characters, such as hyphens and parentheses, currency characters, and date separators.
The control also works well when bound to a data source.