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):
Set the properties for the various controls as shown in Table 1.
Table 1. Properties values for the various controls
||"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
Set(ByVal value As String)
txtE_Name.Text = value
Public Property Email() As String
Set(ByVal value As String)
txtE_Email.Text = value
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) _
.BackColor = Color.LightYellow
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.