Windows Forms: Validating TextBoxes

This sample shows how you can create a set of special purpose text boxes that
are designed for a specific type of input. This sample includes text boxes
for e-mail addresses, IP addresses, phone numbers, and social security numbers.
If the user enters text in the text box that does not look like the specified
type of data, then the textbox will turn the text red.

Each textbox inherits a base "RegExTextBox" class. This class is responsible
for validating the contents of a text box against a given regular expression.
You can learn more about regular expressions by searching the MSDN help for
".NET Framework Regular Expressions".


Featured Highlights

Inheritance is used to modify the behavior of an existing control (the TextBox)

The controls on this form utilize anchoring so that the form resizes intelligently
without the developer having to write any code.

When the user clicks the "Validate" button on the main form, the code loops
through the controls collection to find all the RegExTextBox controls, then
casts them to that base type to call their IsValid method.

Requirements

Microsoft Visual Studio.NET Professional or greater.
Windows 2000 or Windows XP.

Running the Sample

Simply Press F5.

See Also

Regular Expressions