In the previous post I gave a brief introduction on what is a UITypeEditor and what you can get from using it, this part I will show you how to implement one.
So here are the steps:
- Define a class that derives from System.Drawing.Design.UITypeEditor.
- Override GetEditStyle to return a supported UITypeEditorEditStyle.
- Override EditValue and pass any controls necessary to the IWindowsFormsEditorService.
- Override GetPaintValueSupported.
- Override PaintValue if the editor supports painting.
- Override IsDropDownResizable if the editor is resiazble.
Now we will go through the steps one by one, the example I will introduce here will be another ColorEditor, I will use the ColorWheel introduced and explained in this MSDN magazine article.
The final editor that we will make will look like this
[more]
Step 1
[code:c#]
public class ColorWheelEditor : UITypeEditor
[/code]
Step 2
[code:c#]
public override UITypeEditorEditStyle GetEditStyle(System.ComponentModel.ITypeDescriptorContext context)
{
return UITypeEditorEditStyle.DropDown;
}
[/code]
Step 3
[code:c#]
public override object EditValue(System.ComponentModel.ITypeDescriptorContext context, IServiceProvider provider, object value)
{
IWindowsFormsEditorService iwefs = provider.GetService(typeof(IWindowsFormsEditorService)) as IWindowsFormsEditorService;
Color c;
using (ColorWheelContainer cwc = new ColorWheelContainer(iwefs))
{
cwc.Color = (Color)value;
iwefs.DropDownControl(cwc);
if (cwc.Result == DialogResult.OK)
{
c = cwc.Color;
}
else
{
c = (Color)value;
}
}
return c;
}
[/code]
Here, I need to introduce you to the IWindowsFormsEditorService .
Namespace | System.Windows.Forms.Design | ||||||
Assembly | System.Windows.Forms | ||||||
Methods | 3 | ||||||
|
Step 4
[code:c#]
public override bool GetPaintValueSupported(System.ComponentModel.ITypeDescriptorContext context)
{
return true; // we will use the picked color and fill the rectangle.
}
[/code]
Step 5
[code:c#]
public override void PaintValue(PaintValueEventArgs e)
{
Color c = (Color)e.Value;
e.Graphics.FillRectangle(new SolidBrush(c), e.Bounds);
}
[/code]
Step 6
[code:c#]
public override bool IsDropDownResizable
{
get
{
return false;//we don't want it to be resizable
}
}
[/code]
Now don't forget to associate the editor with the color property using the Editor attribute.
[code:c#]
[Editor(typeof(ColorWheelEditor),typeof(UITypeEditor))]
public Color ColorProperty
[/code]
In this part I showed how to implement a UITypeEditor that appears in a Drop Down. See you soon.
Leave a Reply