Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Build a Property Editor That Can Edit Any .NET Type : Page 2

This article describes how to create a Windows Forms control that leverages the power of the .NET Framework's UITypeEditor and TypeConverter classes at runtime to create a control that can display and edit the values of all .NET types.


advertisement
Design-time Features: UITypeEditor and TypeConverter
When you select a control in the Visual Studio.NET form designer, the Property Grid displays the properties of the selected control. Users can enter some property values only by typing text; but others, such as the BackColor property, provide a UI-based interface that appears or "drops down" when users click a button in the Property Grid row as illustrated in Figure 5.
 
Figure 5 BackColor Property Editor: Note that when a property has a dropdown editor, users can either enter the text value in the property field or pick from the dropdown list, and that the dropdown list can be a complex dialog in its own right.
For such properties the framework must convert the value of the property to and from a string to allow in-place text editing. The .NET framework's TypeConverter feature performs those conversions. You can use the TypeConverter class to convert from any type to any other allowable type, but in this control you'll use a TypeConverter to convert the edited value to and from a string.

At design time, the UITypeEditor feature provides a user interface to edit some types. For example, the design-time editor shown in Figure 5 lets users select a Color object). The sample control's implementation is based on the UITypeEditor and TypeConverter classes described in the following sections.

UITypeEditor
A UITypeEditor can display in one of three different styles defined by the UITypeEditorEditStyle enumeration:

  • DropDown: Users edit the value via a drop-down dialog box.
  • Modal: Users edit the value through a modal dialog box.
  • None: The editor does not provide a special user interface for editing.
The GenericValueEditor control uses this information to determine whether to display a button to drop the editor. When the UITypeEditor style is DropDown, the control shows a drop-down arrow. When the style is Modal, the control shows an ellipsis (…) button.


The UITypeEditor.EditValue method handles the user interface, user input processing, and value assignment. The GenericValueEditor calls this method to launch the editor when the user clicks the button. UITypeEditor provides additional methods that offer support for painting a value representation:

  • The GetPaintValueSupported method indicates whether the editor supports displaying the value representation.
  • The PaintValue method implements the display of the value representation.
The GenericValueEditor control uses these two methods to paint the edited value on the left edge of its text box when appropriate (such as for color values). TypeConverter
You use the TypeConverter class to allow in-place editing of the value in the text box. When a user selects a value from some visual representation, the TypeConverter converts the edited value to a string. Similarly, when a user changes the string value in the text box the TypeConverter converts that string back to a value of the edited type.

In addition to converting values, the TypeConverter may also provide a list of standard values appropriate to the type for which it is designed. For example, the TypeConverter for the Color type returns the list of all named and system colors defined in the .NET framework. Similarly, a type converter for an enum type provides the standard values and named constants defined by the enumeration. When the TypeConverter provides standard values for a type and no UITypeEditor is available, the GenericValueEditor lets users select one of the values from a drop-down list as illustrated in Figure 6:

 
Figure 6. GenericValueEditor: To edit most common enum types, you obtain the list of standard value names from the enum and place those in the list.



Comment and Contribute

 

 

 

 

 


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

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date