Creating a Custom Control and Custom UITypeEditor in VB - Part 1
Testing the Custom Control
Controls are just classes. We can create an instance of the RegexTextBox directly, provide an Expression value, and test the control before adding it to the Toolbox. (Although adding a control to the Toolbox is pretty easy in .NET, I like to give controls a quick dry run before installing them.)
To test the RegexTextBox, create a new Windows Forms application. Add a reference to the class library containing the RegexTextBox. The code in Listing 2 demonstrates an example of dynamically constructing and testing the RegexTextBox in a Windows Forms application.
1: Public Class Form1 2: Inherits System.Windows.Forms.Form 3: 4: [ Windows Form Designer generated code ] 5: Private Sub Form1_Load(ByVal sender As System.Object, _ 6: ByVal e As System.EventArgs) Handles MyBase.Load 7: 8: Dim Control As CustomControls.RegexTextBox = _ 9: New CustomControls.RegexTextBox() 10: Control.Expression = "^\d+$" 11: Controls.Add(Control) 12: Control.Text = "w" 13: 14: End Sub 15: End Class
I added a reference to the CustomControls.dll. Line 8 declares a variable and line 9 creates an instance of the RegexTextBox. Line 10 adds a regular expression. This expression means that we are looking for a contiguous string of digits (\d+) with no preceding (^) or trailing ($) characters. Line 11 adds the RegexTextBox instance to the Form's Controls collection, which allows the control to be displayed and receive messages, and line 12 assigns the Text property some invalid text. Simply closing the form will cause the Validating behavior to occur. Because "w" is not a contiguous string of digits, the AcceptMismatch dialog is displayed. If you click OK the application closes; if you click Cancel then the application does not close.
Before releasing the control to the general public, you will want to devise some more positive and negative tests. For example, test to make sure the control works when a valid entry is assigned to Text and when the Expression is a null string. What happens if the Expression is not a valid regular expression?
Installing the Custom Control
Installing controls is straightforward. Assuming you added your custom control to a Class Library, you can install the control from the Toolbox. Right-click on the Toolbox and select Customize Toolbox. Using the Customize Toolbox dialog, pick the .NET Framework Components tab and browse to your control's DLL. After you pick the DLL and click OK, the control will show up in the Toolbox. You can also add a tab from the Toolbox's context menu for your custom controls.
To add additional fit and finish, you will probably want to provide a description for your new properties. (Refer to the DescriptionAttribute in the Visual Studio .NET help.) You may also want to provide a custom bitmap, instead of using the default Gear bitmap. Custom bitmaps can be associated with custom controls with the ToolboxBitmapAttribute. You can look up these features in the Visual Studio .NET help or pick up a copy of The Visual Basic .NET Developer's Book, which has a comprehensive section on building professional, custom components and controls.
Custom components and controls do not have to be complex to be valuable. By associating a regular expression with a TextBox, we can offer a tremendous amount of validation capability through the Regular Expression language in .NET. The additional code is relatively easy; the results are more robust and potentially bullet-proof user input. (All you have to do is provide the regular expressions.)
In the second half of this article (later this month), I will demonstrate how to create a custom type editor, a UITypeEditor. The UITypeEditor is used when custom control editing at design time is needed. For our purposes, we will create a UITypeEditor that facilitates experimentation with the regular expression language in .NET at design time. Stay tuned.
About the Author
Paul Kimmel is a freelance writer for Developer.com and CodeGuru.com. Look for his recent book "Advanced C# Programming" from McGraw-Hill/Osborne on Amazon.com. Paul Kimmel is available to help design and build your .NET solutions and can be contacted at email@example.com.
# # #
Page 2 of 2