To associate a UITypeEditor with a custom control we use the EditorAttribute applied to the property we will be editing. Listing 3 shows a partial listing of the RegexTextBox custom control with the proper placement of the EditorAttribute (line 12).
Listing 3: A partial listing of the RegexTextBox with the EditorAttribute applied.
1: Imports System.Windows.Forms 2: Imports System.Text.RegularExpressions 3: Imports System.ComponentModel 4: Imports System.Drawing.Design 5: Imports System.Windows.Forms.Design 6: 7: Public Class RegexTextBox 8: Inherits TextBox 9: 10: Private FExpression As String 11: 12: <Editor(GetType(RegexTextBoxEditor), GetType(UITypeEditor)), _ 13: Description("A regular expression")> _ 14: Public Property Expression() As String 15: Get 16: Return FExpression 17: End Get 18: Set(ByVal Value As String) 19: FExpression = Value 20: End Set 21: End Property 22: 23: Protected Overrides Sub OnValidating( _ 24: ByVal e As CancelEventArgs) 25: If (FExpression = String.Empty OrElse _
The EditorAttribute takes a couple variations of arguments, both of which indicate the editor and that editor’s base class. The editing capability will manifest itself as extended behavior in the Properties window. If the UITypEditorEditStyle is Modal then a button with an ellipses will be displayed in the specific property’s edit field in the Properties window (see figure 2).
Figure 2: A property with a modal dialog editor will have a button displayed in the edit bar, as shown.
You have the option of entering a literal value or using the button to display the dialog (shown in figure 1).
Testing the Type Editor
Clearly testing the editor can be accomplished by clicking the elliptical button shown in figure 2. I encourage you to perform this kind of direct testing. Click the button and enter some text in the Input field and Expression field of the editor (see figure 1).
Direct testing is mandatory and will give you a good opportunity to explore regular expressions in this case. I would also like to take a minute to tell you about www.nunit.org. This site contains a free GUI and console implementation of NUnit. Model after JUnit, this testing suite makes it easy to create automated tests and the implementers did a top-notch job with this product. Download a free copy of NUnit and give it a try; it is a great way to automate testing for .NET.
Some of my readers may not know—and may not be happy to hear—that for years Borland’s Delphi has provided excellent support for building custom controls. Those who have had time to explore outside of VB6 have known this. As luck would have it, a key architect of Borland’s VCL has been instrumental in bringing us .NET, and as never before VB .NET programmers have a tool that makes it easy and a ton of fun to build professional custom controls.
If you are going to create custom controls, think small incremental changes. If you need some specialized editing then implement a custom UITypeEditor.
There a lot of additional capabilities in .NET, especially when it comes to implementing custom components and controls. To learn more about this subject pick up a copy of my upcoming book, The Visual Basic .NET Developer’s Book from Addison-Wesley.
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 protected].
# # #