Creating custom controls is a key technique in .NET development. This article by Matthew is the second of a two part series where he looks at how to create a custom Windows Form control that behaves properly in Visual Studio .NET.
Custom Controls and Design-Time Support: Part 2/2 - UITypeEditors (Page 5 of 7 )
The Properties window does a solid job of providing support for all common data types. Basic data types like strings and numbers are all seamlessly supported. If you create a property that uses a custom enumeration, the Properties window automatically provides a drop-down list with the values from the enumeration.
In addition to this basic level of support, you may have noticed that some richer object types have additional design-time support.
For example, if you create a Property that has a Font or Color data type, a special color picker or font selection dialog will be used in the Properties window. Similar magic happens if you create a Collection property.
This support is provided through .NET UITypeEditor classes, which are special components that provide the design-time user interface that allows the programmer to set complex properties.
Prebuilt UITypeEditors
The base UITypeEditor class is found in the System.Drawing.Design namespace. You can inherit from this class to create your own custom UITypeEditors, or you can use one of the derived classes that are provided with the .NET framework.
These classes are listed below (UITypeEditors that are only usable with specific web controls have been omitted-see MSDN for more information about those).
Class
Description
System.ComponentModel.Design.ArrayEditor
Edits an array by allowing the programmer to enter a list of strings. Used automatically for supported arrays.
System.Drawing.Design.FontEditor
Allows the programmer to select and configure a font. Used automatically for font properties.
System.Drawing.Design.ImageEditor
Allows the programmer to create an Image object by selecting a bitmap from an open file dialog. Used automatically for image types.
System.Web.UI.Design.WebControls.RegexTypeEditor
Allows the programmer to choose a regular expression from a list of common choices. This UITypeEditor works with string properties.
System.Windows.Forms.Design.AnchorEditor
Allows the Anchor property to be set at design time.
System.Windows.Forms.Design.FileNameEditor
Allows a fully qualified filename to be set by choosing a file from an open file dialog box. This UITypeEditor works with string properties.
Depending on the data type of your property, .NET may use the corresponding UITypeEditor automatically (for example, with a Font).
On the other hand, some UITypeEditors do not have dedicated types. An example is the RegExTypeEditor, which allows the programmer to choose a common regular expression for a control property. The regular expression is stored as an ordinary string, so it needs to be explicitly associated with the appropriate UITypeEditor.