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


Customize and Extend Windows Forms Controls : Page 4

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.

Developing User Controls
In the previous section, you saw how to extend the built-in TextBox control with various filters and formatting. You can now easily use the newly enhanced TextBox control to perform all sorts of input filtering and not worry about the innards of how it works.

Besides extending individual Windows Forms controls, you can also group them into one single control. This is accomplished by the project type known as a Windows Control Library. Using the enhanced TextBox control created in the previous section, you might want to combine a few of these controls together to form a new control, for example, a control that contains two TextBox controls to allow the user to enter his name and email address. Let’s see how this can be done.

Using the same solution, add a new Windows Control Library project to the existing solution and name the project WindowsControlLibrary1. You can see the default design of the control in Figure 7.

Figure 7. The design surface of the user control is shown.
Figure 8. Populate the user control with a label and enhanced TextBox controls.

Populate the design surface with the following controls (see also Figure 8):

  • Label
  • EnhancedTextBox

Set the properties for the various controls as shown in Table 1.

Table 1. Properties values for the various controls

Control Property Value
UserControl1 BorderStyle FixedSingle.
txtE_Name FilterType AlphabetsOnly
txtE_Email RegularExpression "^[\w-\.]{1,}\@([\da-zA-Z-]{1,}\.){1,}[\da-zA-Z-]{2,3}$"
txtE_Email RegularExpressionErrorMessage "Email is incorrect."

Switch to the code-behind of the user control by double-clicking on its design surface. Here, you will add two member variables and two properties to allow the user to set the user name as well as email address:

Public Class UserControl1 Private _Name As String Private _Email As String Public Property UserName() As String Get Return txtE_Name.Text End Get Set(ByVal value As String) txtE_Name.Text = value End Set End Property Public Property Email() As String Get Return txtE_Email.Text End Get Set(ByVal value As String) txtE_Email.Text = value End Set End Property

In the design surface of the user control, when you double-click on the txtE_Email control, the Err event handler will be automatically created for you (recall that the default event for the EnhancedTextBox control is Err):

Private Sub txtE_Email_Err(ByVal str As System.String) _ Handles txtE_Email.Err With txtE_Email .BackColor = Color.LightYellow End With End Sub

This event would be fired whenever there is an error with the validation. For this instance, you would set the background color of the control to light yellow should the validation fail.

Testing the User Control
You are now ready to test the new user control. Right-click on the UserControl 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…. In the Choose ToolBox Items window, click the Browse… button, navigate to the bin\Debug folder of the project, and select WindowsControlLibrary1.dll. Click Open. The user control should now be visible in the ToolBox. Drag and drop it onto the bottom of Form1 (see Figure 9).

Figure 9. The new user control is in action.
Figure 10. The error message to inform the user that the input validation has failed is shown in a yellow balloon.

Try entering your name in the first control and press Tab to go to the next. If you enter an invalid email address and then press Tab to cause it to lose its focus, a balloon will appear and the background color of the control is changed to light yellow (see Figure 10).

In this article, you have seen how to extend Windows Forms controls and package them as new controls. Extending Windows Form controls can save a lot of time when you are using the same control over and over again. This is especially true in big development projects where you have a lot of repetitive functions.

Wei-Meng Lee is a Microsoft MVP and founder of Developer Learning Solutions, a technology company specializing in hands-on training on the latest Microsoft technologies. He is an established developer and trainer specializing in .NET and wireless technologies. Wei-Meng speaks regularly at international conferences and has authored and coauthored numerous books on .NET, XML, and wireless technologies. He writes extensively on topics ranging from .NET to Mac OS X. He is also the author of the .NET Compact Framework Pocket Guide, ASP.NET 2.0: A Developer's Notebook (both from O'Reilly Media, Inc.), and Programming Sudoku (Apress). Here is Wei-Meng's blog.
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.