Since we are canceling, the focus will remain on the Text Box until the user enters the correct format. You can have a label to display the error message or change the foreground color of the Text Box or use a Tool Tip control to display a popup message.
A good solution is to use the Error Provider control.
We will not go into details on how to use Regular Expressions, since it’s an extensive subject unto itself.We recommend referring to Power Shell’s about_Regular_Expressions help topic or type “Power Shell Regular Expressions” in a search engine for more information.In addition, Power Shell has built in support for regular expressions in the form of the –match operator.Example use of a regular expression in Power Shell: In this example we are matching any string that contains the word “shell”.A good user interface will validate user input to ensure it is in the correct format.
This may come in the form of simply checking if there is an entry for a field, such as a name or a more complex task such as validating an email address.
Thankfully Win Forms has built in mechanisms to help facilitate data validation.
Each control has the following events and properties that are used to validate a form. Validation on a control is triggered when the control loses focus. Cancel Event Args] object as a parameter to the event block, which you can access this by using the in the Validating event, all events that would usually occur after the Validating event are suppressed.
There is no need to validate a control that loses focus when the user wishes to cancel out of the form; therefore, the button’s Causes Validation property can be set to false.
As it turns out, validating controls depends on what type of control and the format of the data you wish to validate.
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.