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 6

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
Dropping the UITypeEditor
Now that you know how to edit the value in the text box using the type converter, you can implement a way to edit the value using a UITypeEditor. The UITypeEditor class defines the following EditValue method that you should use to launch an editor for editing the value.

public object EditValue(IServiceProvider provider, object value);

The value parameter is the value to edit, and the result returned by this method is the new edited value, but what's that first IServiceProvider parameter? The .NET framework documentation says it's "An IServiceProvider that this editor can use to obtain services." How helpful. This does not explain what you need to do. In fact, if you explore the UITypeEditor documentation, you'll find that it requires a service interface called IWindowsFormsEditorService to show the drop-down control. However, the .NET framework contains no public implementation of such a service that you can use. You have to create a class to provide such a service. You'll see how to do that later. For the moment, suppose that you have such a service stored in a class field called editorService. Dropping the editor is then very easy. The DropEditor method does the work:

void DropEditor() { UITypeEditor editor = Editor; if (editor != null) { try { Value = editor.EditValue( editorService, Value); } catch (Exception) {} } }

The code simply calls the EditValue method of the UITypeEditor and sets the Value property of the control to the result.

Unfortunately, many .NET types do not define a UITypeEditor. For example, none of the Enumeration types (subclasses of the .NET Enum class) define a UITypeEditor—but they do have a TypeConverter that can convert the named constants defined by the enum to and from a string representation. The TypeConverter provided for enum types can also return the list of available constants through the GetStandardValues method. For example the little program below uses the GetStandardValues method to display the list of available constants in the Console window.



TypeConverter converter = TypeDescriptor.GetConverter(typeof(DayOfWeek)); foreach (object obj in converter.GetStandardValues()) { Console.WriteLine(obj); }

When you run that code, you'll see all the named constants defined by the DayOfWeek enum: Sunday, Monday, etc. You can use this TypeConverter feature to provide a UI for editing enum types; you just have to create an internal UITypeEditor yourself for those types. The created UITypeEditor can drop down a ListBox containing all the standard values defined for the type. This new UITypeEditor is called a StandardValuesUIEditor. For more details on the UITypeEditor, refer to the source. The DropEditor method code is too complex to show for enum types; see the sample code for more information:

void DropEditor() { UITypeEditor editor = Editor; if ((editor == null || editor.GetEditStyle() == UITypeEditorEditStyle.None) && hasStandardValues) { if (standardValuesUIEditor == null) standardValuesUIEditor = new StandardValuesUIEditor(this); editor = standardValuesUIEditor; } if (editor != null) { try { Value = editor.EditValue(editorService, currentValue); } catch (Exception) {} } }

You call the DropEditor method when the user clicks the arrow button on the GenericValueEditor. An event handler on the button that calls DropEditor makes the call, but you're still missing the mysterious IWindowsFormsEditorService.



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