Login | Register   
RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


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

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.

Defining the Edited Type, UITypeEditor, and TypeConverter
The EditedType property defines the type of objects that the GenericValueEditor will edit. Setting this property also sets the TypeConverter and the UITypeEditor associated with the type. To get the TypeConverter and UITypeEditor associated with a type, use the .NET framework's TypeDescriptor class, which provides information about a type.

To store the information define three new fields named editedType, converter, and editor. Here's the EditedType property definition:

private Type editedType; private UITypeEditor editor; private TypeConverter converter; public Type EditedType { get { return editedType; } set { if (value == null) throw new ArgumentNullException( "type should not be null"); if (editedType != value) { editedType = value; converter = TypeDescriptor.GetConverter(editedType); editor = (UITypeEditor)TypeDescriptor.GetEditor( editedType, typeof(UITypeEditor)); OnConverterOrEditorChanged(); } } }

You must also define two additional properties called Converter and Editor that let you change the TypeConverter and UITypeEditor:

public TypeConverter Converter { get { return converter; } set { if (converter != value) { converter = value; OnConverterOrEditorChanged(); } } } public UITypeEditor Editor { get { return editor; } set { if (editor != value) { editor = value; OnConverterOrEditorChanged(); } } }

The OnConverterOrEditorChanged method fires whenever the editor or converter changes. It stores information related to the current converter and editor.

private bool paintValueSupported; private bool hasStandardValue; private bool hasButton; private void OnConverterOrEditorChanged() { paintValueSupported = editor != null && editor.GetPaintValueSupported(); hasStandardValues = converter != null && converter.GetStandardValuesSupported() && converter.GetStandardValues().Count != 0; hasButton = (editor != null && editor.GetEditStyle() != UITypeEditorEditStyle.None) || hasStandardValues; editorButton.IsDialog = editor != null && editor.GetEditStyle() == UITypeEditorEditStyle.Modal; LayoutSubControls(); UpdateTextBoxWithValue(); }

The preceding code fragment defines three new fields for the GenericValueEditor:

  • The paintValueSupported field indicates whether the editor can paint the edited value.
  • The hasStandardValues field indicates whether the type supports standard values.
  • The hasButton field indicates whether the GenericValueEditor should display a button.
You use this information to show or hide the subcontrols in the LayoutSubControls method. The button becomes visible when the UITypeEditor has a Modal or DropDown style (in which case the button launches the UITypeEditor) or when you can select from a set of standard values for the type (in which case the button drops a list box that lets you select one of the standard values). The PreviewControl becomes visible only when the paintValueSupported field is true.

Comment and Contribute






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



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