Click here to Skip to main content
11,920,353 members (41,872 online)
Click here to Skip to main content
Add your own
alternative version


71 bookmarked

Description Enum TypeConverter

, 3 Mar 2004
Rate this:
Please Sign up or sign in to vote.
This article explains how to manage to make a TypeConverter for enums using its descriptions.

Click to Enlarge

Click Image to Enlarge


I had been looking everywhere about information on TypeConverters, specially their uses for the PropertyGrid. There's not much documentation about it, and I kinda needed it for some work stuff. That said, I'm presenting you here a simple EnumConverter which allows the use of the DescriptionAttribute to convert from and to the Enum values. This is great for presentation on a PropertyGrid, as you don't want either your Enum names appearing all over an application, neither you.


I was in the process of creating a 3D engine in C#, and I was making tools to edit materials and a lot of more stuff. Each material, texture, etc, can have tons of properties, and I was aiming to discover a way of making my UI fast and with no mistakes. The usual way (making labels and textboxes/comboboxes/whatever) would be just too time-consuming, and I was (am) on a tight schedule. So I came across the PropertyGrid.

As the C# newbie I am, I started fiddling around the System.ComponentModel attributes to make my classes browsable on a PropertyGrid, so I first made 'Editable' wrappers which I'd only use on the tools (no need to overload the engine with things I won't use afterwards). This is a partial IBitmap editable class, so you know what I'm talking about:

namespace Immerse.Editables
  public class EditableIBitmap
    private IBitmap bmp;
    public EditableIBitmap(IBitmap bmp) 
      this.bmp = bmp;

    public IBitmap Bitmap { get { return bmp; } } 

    [Category("Bitmap Properties"),Description("Bitmap Name")]
    [DefaultValue("Unnamed Bitmap")]
    public string Name { get { return bmp.Name; } }
    [Category("Bitmap Properties"),Description("Bitmap Width")]
    public int    Width { get { return bmp.Width; } }
    [Category("Bitmap Properties"),Description("Bitmap Height")]
    public int    Height { get { return bmp.Height; } }
    [Category("Bitmap Properties"),Description("Bitmap Depth")]
    public int    Depth { get { return bmp.Depth; } }
    [Category("Bitmap Properties"),Description("Bits per Pixel")]
    public int    Bpp { get { return bmp.Bpp; } }
    [Category("Bitmap Properties"),Description("Number of Dimensions (1-3)")]
    public int    Dimensions { get { return bmp.Dimensions; } }

    [Category("Bitmap Properties"),Description("Texture Tiling U")]
    public TextureTileTypes TileTypeU 
    { get { if(bmp.TileType.Length > 0) 
               return bmp.TileType[0]; 
            else return TextureTileTypes.Tile; } }
    [Category("Bitmap Properties"),Description("Texture Tiling V")]
    public TextureTileTypes TileTypeV 
    { get { if(bmp.TileType.Length > 1) 
                return bmp.TileType[1]; 
            else return TextureTileTypes.Tile; } }
    [Category("Bitmap Properties"),Description("Texture Tiling W")]
    public TextureTileTypes TileTypeW 
    { get { if(bmp.TileType.Length > 2) 
                return bmp.TileType[2]; 
            else return TextureTileTypes.Tile; } }

So, this is not complete, but might give you a nice idea about how I got my editable wrappers implemented. Now, when put on a PropertyGrid, I'd get all the values. There's no setters put on this example, but you get the idea (I don't have them implemented like this, this is just an example).

So I made my own ColorPicker converter and some more stuff. Everything was working great, since it came the time of 'touching up' so it'd have a nice look, and was actually usable for other people than me. The main problem came with all different Enums I had on the program.

For example, TextureTileTypes, is an Enum like:

public enum TextureTileTypes

So in the PropertyGrid, my values would look like 'Tile','Clmp','ClmpB','ClmpE'. Not pretty much user-friendly, uh?

Trying to find some documentation in the net, everyone suggested that I should make a "TextureTileTypeConverter" and roll my own values... that's fine for one Enum, but not quite right for the tons of Enums I have spread all over my engine on possibly editable properties.

So, using the System.ComponentModel.DescriptionAttribute, I got to do this:

public enum TextureTileTypes
  [Description("Tile")]            Tile,
  [Description("Clamp")]           Clmp,
  [Description("Clamp to Border")] ClmpB,
  [Description("Clamp To Edge")]   ClmpE,

They're, much easier to edit, much easier to code, and definitely no hassle to get a human-readable description of it. Now I just needed a way for the PropertyGrid to display the Description names, while still retaining the normal enum values for the setters. And that converter is what I'm presenting to you now.

Using the code

The code should be self-explanatory for anyone who has ever done any kind of custom TypeConverter for a PropertyGrid. You just set the ConverterAttribute of your editable class to typeof(EnumDescConverter), and that should do the trick.

Notice the following function:

public static string GetEnumDescription(Enum value)
  FieldInfo fi= value.GetType().GetField(value.ToString());
  DescriptionAttribute[] attributes =
    (typeof(DescriptionAttribute), false);
  return (attributes.Length>0)?attributes[0].Description:value.ToString();

That should return the human-readable description set to the Enum value, if it has any, if it doesn't, then it just returns the Name as if you had done a ToString().

The other way around:

public static string GetEnumName(System.Type value, string description)
  FieldInfo [] fis = value.GetFields();
  foreach(FieldInfo fi in fis)
    DescriptionAttribute [] attributes =
      (typeof(DescriptionAttribute), false);
      if(attributes[0].Description == description)
        return fi.Name;
  return description;

So you can use those separately to retrieve any human-readable-form enum value, anywhere in your code. That should be useful enough by itself.

About (the juicy stuff), using the TypeConverter, just include the source file (EnumDescConverter.cs) somewhere in your project, and for your editable class (or however you want the browsing to be done), add typeof(Jcl.Util.EnumDescConverter), like this:

public MyEnumType MyProperty
{ get { return myEnumVariable; } set { myEnumVariable = value; } }

Of course, you are 100% allowed to change the namespace and put it on your own Smile | :)

Points of Interest

There are pretty much unbearable bugs appearing up when you start writing TypeConverters (as well as extending any other part of the .NET Framework), and most of them are very hard to find, as the documentation is not specially good yet, and there is no source for the .NET Framework, so there are many things going on the background which you can't just trace, as they are event-driven in the application. Once you get the rid of it though it's pretty easy to catch up.


You can use the code for free, you may include it in your applications, and you might modify it. Basically, do whatever you want with it. If you get to use it in your application, make sure you notify me about it, and credit would be appreciated, while not required.


First version of this document on 25th Feb 2004. Will try to make an example project soon.


You may contact me for anything related to this article at jcl at vmslabs dot com.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Javier Campos
Chief Technology Officer La Boutique del Hogar
Spain Spain
I've been programming since 1994 and done projects in x86 Assembler, C, C++, Delphi and lately C#.

You may also be interested in...

Comments and Discussions

GeneralRe: DotNet 2.0 Enhancement? Pin
yxjie223-May-11 22:12
memberyxjie223-May-11 22:12 
GeneralNot working with DataGrid ComboBox Pin
jimbutler10116-Nov-06 1:48
memberjimbutler10116-Nov-06 1:48 
AnswerRe: Not working with DataGrid ComboBox Pin
Bernd Wißler12-Dec-06 23:16
memberBernd Wißler12-Dec-06 23:16 
GeneralAnother Enhancement Pin
This is retarted26-Apr-06 1:24
memberThis is retarted26-Apr-06 1:24 
GeneralEnhancement Pin
mickyddoyle16-Mar-06 4:34
membermickyddoyle16-Mar-06 4:34 
GeneralRe: Enhancement Pin
Javier Campos16-Mar-06 5:03
memberJavier Campos16-Mar-06 5:03 
GeneralVB not works! Pin
carloqueirolo3-Oct-05 22:47
susscarloqueirolo3-Oct-05 22:47 
GeneralRe: VB not works! Pin
Javier Campos16-Mar-06 5:04
memberJavier Campos16-Mar-06 5:04 
GeneralEnum.ToString Method Pin
Ali Demirkaya15-Sep-05 4:38
memberAli Demirkaya15-Sep-05 4:38 
GeneralRe: Enum.ToString Method Pin
Javier Campos15-Sep-05 12:48
memberJavier Campos15-Sep-05 12:48 
GeneralQuestion about object names. Pin
Don Riesbeck14-Sep-05 8:06
memberDon Riesbeck14-Sep-05 8:06 
GeneralRe: Question about object names. Pin
Javier Campos14-Sep-05 8:22
memberJavier Campos14-Sep-05 8:22 
GeneralRe: Question about object names. Pin
Don Riesbeck14-Sep-05 8:47
memberDon Riesbeck14-Sep-05 8:47 
GeneralRe: Question about object names. Pin
MichalBebas22-Jun-06 0:40
memberMichalBebas22-Jun-06 0:40 
GeneralRe: Object (Property) names - solution Pin
Bobbie Boy27-Oct-05 10:09
memberBobbie Boy27-Oct-05 10:09 
GeneralPlease update your source here Pin
John Cardinal7-Apr-05 7:47
memberJohn Cardinal7-Apr-05 7:47 
GeneralRe: Please update your source here Pin
Javier Campos7-Apr-05 7:49
memberJavier Campos7-Apr-05 7:49 
GeneralExcellent yet doesn't work :S Pin
andy18013-Feb-07 2:51
memberandy18013-Feb-07 2:51 
GeneralRe: Excellent yet doesn't work :S Pin
Javier Campos13-Feb-07 6:34
memberJavier Campos13-Feb-07 6:34 
GeneralError while selecting in the combobox Pin
Alessandro Fragnani27-Mar-05 10:44
memberAlessandro Fragnani27-Mar-05 10:44 
GeneralRe: Error while selecting in the combobox Pin
Javier Campos28-Mar-05 2:46
memberJavier Campos28-Mar-05 2:46 
QuestionAnother example perhaps? Pin
prcarp3-Mar-05 9:57
sussprcarp3-Mar-05 9:57 
AnswerRe: Another example perhaps? Pin
Javier Campos4-Mar-05 0:57
memberJavier Campos4-Mar-05 0:57 
GeneralRe: Another example perhaps? Pin
Javier Campos4-Mar-05 0:58
memberJavier Campos4-Mar-05 0:58 
GeneralRe: Another example perhaps? Pin
prcarp4-Mar-05 4:46
memberprcarp4-Mar-05 4:46 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.151120.1 | Last Updated 4 Mar 2004
Article Copyright 2004 by Javier Campos
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid