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 3

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.

Implementing the GenericValueEditor
The first step in creating the GenericValueEditor control is to derive a new control, called GenericValueEditor, from the System.Windows.Forms.Control class.

The GenericValueEditor control embeds three subcontrols:

  • A TextBox for editing the value as text
  • An optional Button for dropping the UITypeEditor
  • Another optional control that paints the edited value if the UITypeEditor supports this feature.
You also need to define the type that the control will edit. The default constructor creates a GenericValueEditor for the String type.

Here is the constructor code for the class:

public class GenericValueEditor : Control { private TextBox textbox; private EditorButton editorButton; private PreviewControl previewControl; private boolean autoSize; private EditorService editorService; … public GenericValueEditor() : this(typeof(string)) { } public GenericValueEditor(Type editedType) { SetStyle(ControlStyles.Selectable, true); SetStyle(ControlStyles.FixedHeight, true); autoSize = true; SuspendLayout(); // Text box Control textBox = new TextBox(); InitTextBox(); // Editor button editorButton = new EditorButton(); editorButton.Click += new EventHandler(ButtonClicked); // Paint value box previewControl = new PreviewControl(this); previewControl.Click += new EventHandler(PreviewControlClicked); // Adds the subcontrols Controls.AddRange( new Control[] {previewControl, textBox, editorButton}); // Creates the editor service editorService = new EditorService(this); // Stores the edited type EditedType = editedType; ResumeLayout(); } ... }

This constructor creates the three subcontrols and adds them to the GenericValueEditor (see Figure 7). The first control is the TextBox for editing the string representation. The second control is an instance of the class EditorButton (a subclass of Button) and the last control is a small control that displays the current value (an instance of PreviewControl).

Figure 7. GenericValueEditor Subcontrols: The GenericValueEditor coordinates three controls to provide a preview (for such properties as colors), a TextBox where users can enter the text representation of a value, and an EditorButton that drops a dialog appropriate for the type being edited.
The button is a subclass of the .NET Framework Button class called EditorButton. This button class draws itself as a drop-down arrow when the current UITypeEditor has the DropDown style, or as three dots when the UITypeEditor has the Modal style. The PreviewControl control uses the PaintValue method of the UITypeEditor class.

The GenericValueEditor's constructor also creates an EditorService instance (described later in this article), which handles the process of dropping down the list for the UITypeEditor. Finally, the editor stores the edited type in the EditedType property described below.

The three controls are laid out and made visible or hidden depending on the UITypeEditor or the TypeConverter associated with the edited type.

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