RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


Customize and Extend Windows Forms Controls : Page 3

Everybody who writes native Windows applications knows that the Windows Forms controls provided by Microsoft are a great time-saver for oft-used interface components such as text boxes and drop-down lists. But sometimes we need a reusable form control that does things Microsoft hasn't pre-built for us. Learn to extend existing Windows Forms controls with custom functions that will save you time.

Testing the Extended TextBox Control
You are now ready to test the extended TextBox control. Right-click on the EnhancedTextBox project in Solution Explorer and select Build to compile the project into a DLL.

Back in the WindowsControls project, right-click on the Common Controls tab in Toolbox and select Choose Items… (see Figure 1).

Figure 1. Add a new control to the ToolBox.
Figure 2. Select the EnhancedTextBox.dll.

In the Choose ToolBox Items window, click the Browse… button and navigate to the bin\Debug folder of the project and select EnhancedTextBox.dll (see Figure 2). Click Open.

The EnhancedTextBox control will now appear in the ToolBox. Drag and drop a copy of it onto the default Form1 of the WindowsControls project (see Figure 3).

Figure 3. Drag and drop the EnhancedTextBox control onto Form1.
Figure 4. Locate the properties and event of the EnhancedTextBox control.

Right-click on the newly added EnhancedTextBox control and select Properties. Recall the properties and event that you defined earlier in the EnhancedTextBox project. They can now be found in the Properties window. If you switched to Categorized view, you will find them located under the custom-defined CustomProperties category (see Figure 4). You can set the various properties using the Properties window.

Alternatively, you can also set the properties programmatically through code, like this:

        '---here is one example---
        With EnhancedTextBox1
            .FilterType = EnhancedTextBox.FilterTypes.DecimalNumeric
            .Precision = EnhancedTextBox.Precisions.TwoDecimal
        End With

        '---another example---
        With EnhancedTextBox1
            .RegularExpression = _
            .RegularExpressionErrorMessage = _
               "Email address is not correct"
        End With
To thoroughly test the new extended control easily, I have added some controls to the form (see Figure 5). The form also shows the logical relationships between all the various filters. For example, if you select the DecimalNumeric filter, then only the Precision formatting applies. Alternatively, if you select either the AlphabetsOnly or Alphanumeric filter, then only the EnforceCasing formatting applies. If a regular expression is entered, then the text entered will be validated after it loses its focus.

Author's Note: You can download the sample application accompanying this article.

Figure 5. The screen shot shows testing of the EnhancedTextBox control.
Figure 6. The balloon is used to show the user that the validation failed.

If the text entered does not pass the validation, a balloon will be shown (see Figure 6).

Close Icon
Thanks for your registration, follow us on our social networks to keep up-to-date