ith Visual Studio 2005, developing Windows Forms applications has never been easier. In the user interface area, Microsoft has provided much of the needed functionality in the form of Windows Forms controls, such as the familiar Button and TextBox controls. Using the controls greatly simplifies the developmental processsimply drag and drop the controls, configure their properties, and service all the necessary events.
However, despite the comprehensive set of functionality, there are times where you need to enhance the controls with new features. A good example is the TextBox control. As you are probably aware, the TextBox control allows users to input data into your application. But it does not provide any sort of validation (such as verifying that the input data is an email address) and input filtering (such as restricting input to numbers). To do so, you need to write your own custom validation and filtering logic for each individual TextBox control. Imagine how much work it would be if you needed to validate a dozen instances of this control; you'd have to write a lot of repetitive code.
A better option is to extend the built-in controls so that they can be used as new controls (enhanced with new features). In this article, I will use a specific examplethe TextBox controlto show you how you can extend Windows Forms controls. I will extend the TextBox control so that it will perform the following:
- Restrict data input through filtering. You can specify the type of data the user can enter, such as numeric (integer or decimal numbers), alphabetic characters only, or alphanumeric.
- Enforce casing. You can specify if the entered data will appear in lower or upper case.
- Precision formatting. You can specify if the numbers entered will appear in one, two, or three decimal places.
- Custom validation. You can use regular expression to perform custom validation, such as verifying social security numbers, zip codes, or email addresses.
The second section of this article will show how you can combine Windows Forms controls into one single control (known as a user control).