Custom Controls Design Time Support Part 8: Implementing UITypeEditor

by Amr 1/18/2008 8:15:00 PM

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:

  1. Define a class that derives from System.Drawing.Design.UITypeEditor.
  2. Override GetEditStyle to return a supported UITypeEditorEditStyle.
  3. Override EditValue and pass any controls necessary to the IWindowsFormsEditorService.
  4. Override GetPaintValueSupported.
  5. Override PaintValue if the editor supports painting.
  6. Override IsDropDownResizable if the editor is resiazble.a


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

 

Step 1

public class ColorWheelEditor : UITypeEditor

Step 2

public override UITypeEditorEditStyle GetEditStyle(System.ComponentModel.ITypeDescriptorContext context)
{
   return UITypeEditorEditStyle.DropDown;
}

Step 3

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;
}

Here, I need to introduce you to the IWindowsFormsEditorService .

Namespace System.Windows.Forms.Design
Assembly System.Windows.Forms
Methods 3
 
DropDownControl accepts a parameter of type control that will be shown the drop down, works when the edit style is UITypeEditorEditStyle.DropDown.
CloseDropDown when called closes the drop down, works when the edit style is UITypeEditorEditStyle.DropDown.
ShowDialog accepts a parameter of type Form, which represents the dialog that will be opened, works when the edit style is UITypeEditorEditStyle.Modal.

Step 4

public override bool GetPaintValueSupported(System.ComponentModel.ITypeDescriptorContext context)
{
    return true; // we will use the picked color and fill the rectangle.
}

Step 5

public override void PaintValue(PaintValueEventArgs e)
{
   Color c = (Color)e.Value;
   e.Graphics.FillRectangle(new SolidBrush(c), e.Bounds);
}

Step 6

public override bool IsDropDownResizable
{
   get
   {
     return false;//we don't want it to be resizable
   }
}

Now don't forget to associate the editor with the color property using the Editor attribute.

[Editor(typeof(ColorWheelEditor),typeof(UITypeEditor))]
public Color ColorProperty

In this part I showed how to implement a UITypeEditor that appears in a Drop Down. See you soon.

 

ColorWheelEditor.zip (13.99 kb)

Currently rated 5.0 by 7 people

  • Currently 5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags: , , ,

Design Time Support

Related posts

Comments

1/25/2008 12:37:33 AM

Amr

Thank you very much gabriel that made my day,
I hope to continue in a useful high quality manner.

Amr eg

1/25/2008 2:46:26 AM

gabriel

Great series! Thank you very much for this, very high quality work, and a rockin' way to start up your blog!

gabriel mx

4/12/2008 11:26:39 AM

GeezerButler

Very nice posts!
This is much more helpful than the msdn documentation.

GeezerButler in

5/8/2008 12:14:48 AM

Mahmoud

Thank you Amr. I spent the morning looking for such a complete article and pedagogical article. It was very helpful.

Mahmoud tn

Add comment


(Will show your Gravatar icon)  

  Country flag

[b][/b] - [i][/i] - [u][/u]- [quote][/quote]



Live preview

5/16/2008 2:08:00 PM

Powered by BlogEngine.NET 1.3.0.0
Theme by Mads Kristensen

About the author

Amr Elsehemy Amr Elsehemy
MCSD, MCTS Sql 2005 E-mail me Send mail

Calendar

<<  May 2008  >>
MoTuWeThFrSaSu
2829301234
567891011
12131415161718
19202122232425
2627282930311
2345678

View posts in large calendar

Recent comments

Authors

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2008

Creative Commons
Sign in

Sitemeter