# Snippet C#: Using ColorDialog in a PropertyGrid



## FordGT90Concept (Mar 8, 2009)

Well, it doesn't take very long to figure out that ColorEditor (the default editor System.Drawing.Color is associated with) isn't that great.  The problem is, the better choice (ColorDialog) does does not inherit "UITypeEditor" (it inherits CommonDialog) so it can't be directly used by PropertyGrids.  The solution is to create a new class that inherits UITypeEditor and pops up a ColorDialog on demand.  For this, you'll need four new classes:

1) [MyColor] A class to store the color information.  We cannot use System.Drawing.Color because it ignores the EditStyle property.

2) [MyColorConverter] A class that derives from TypeConverter.  This is needed for DefaultValueAttribute and to ignore the class above when displaying in the PropertyGrid.

3) [MyColorEditor] A class that derives from UITypeEditor. This is needed to show that fancy ColorDialog.

4) [SelectableClass] A class that contains an object of the color which will be selected by the PropertyGrid.


An example of these files have been included in the attached source file (the name of the file is in the brackets above).

MyColor is a relatively generic class.  The only thing to really pay attention to here is the TypeConverter attribute:


```
[TypeConverter(typeof(MyColorConverter))]
```
In short, this tells it to use "MyColorConverter" if it wants to convert it.  The reason for needing this is mostly cosmetic and not absolutely necessary but still, it is a good thing to have.


Looking at the MyColorConverter class, you'll find only two methods: ConvertFrom and ConvertTo.  Again, these aren't too complex.  All it does is check for the incoming type (string or MyColor) and convert it to other.  If you ConvertFrom a string, it will give you a MyColor class (I used new MyColor(string)).  If you ConvertTo a string, it will take an instance of MyColor and spit out a string that represents it (I used MyColor.ToString()).

ConvertFrom is invoked by the DefaultValueAttribute.  The advantage of this is the default value is no longer bold in the PropertyGrid.

ConvertTo is invoked by the PropertyGrid itself so that it displays "0 0 0" instead of "ColorDialogExample.MyColor"


Now to have a look at MyColorEditor which also only has two methods: GetEditStyle and EditValue.  GetEditStyle only has three options to return: Modal, DropDown, and None.  Modal (has a [...] button) basically means a dialog should pop up and prompt the user for a new value.  DropDown (has a [\/] button) drops down a list of options.  If you enable DropDown you should set override IsDropDownResizable as well.  None means no button at all will appear so all the user can do is a simple text edit to the property.

EditValue is what happens when someone clicks on the button.  It should get input from the user and return the new value that was entered.



Lastly, we have the SelectableObject class which is in turn loaded by the Main form.  The important line to note from this class is the following:


```
[EditorAttribute(typeof(MyColorEditor), typeof(UITypeEditor))]
```
This tells it to use our custom MyColorEditor instead of the default editor (ColorEditor).  Right below that line is the DefaultValueAttribute discussed earlier and some other useful items (category and description).


That's pretty much it.  Ask if you need help.

Here is what the final app looks like:


----------



## Braveheart (Mar 9, 2009)

very nice.


----------

