Click here to Skip to main content
11,930,788 members (51,212 online)
Click here to Skip to main content
Add your own
alternative version


35 bookmarked

Bit Flags Type Converter

, 20 Jun 2006 CPOL
Rate this:
Please Sign up or sign in to vote.
An implementation of TypeConverter which allows you to edit bit flag enumerations in the PropertyGrid.

Sample Image


I am a component developer at 10Tec Company. While developing one of our components, iGrid.NET, I came across a problem: the PropertyGrid control does not provide you with a comfortable-enough editor for enumerations marked with the Flags attribute (enumerations that can be treated as bit fields; that are a set of flags). The PropertyGrid treats them as usual enumerations, and allows you to select a single field from the drop-down list or enter a value as text. So, I decided to implement our own TypeConverter which would help to edit bit field properties.

There can be a few solutions to this problem, for example, we could show a check list box in the drop-down list. My solution is to show flags as sub-properties.

Using the code

To assign a custom type converter to a property, you should use the TypeConverterAttribute attribute:

public class TestClass
  public FontStyle FontStyle

The parameter passed to the TypeConverterAttribute attribute should be inherited from the TypeConverter class or one of its descendents.

Our type converter is named FlagsEnumConverter, and is inherited from the standard .NET EnumConverter class:

internal class FlagsEnumConverter: EnumConverter
  public override PropertyDescriptorCollection 
         GetProperties(ITypeDescriptorContext context, 
         object value, Attribute[] attributes)
  public override bool GetPropertiesSupported(ITypeDescriptorContext context)

The TypeConverter has several overridable properties and methods which allow you to customize the appearance of a property in the PropertyGrid. In our type converter, I overrode two methods: GetPropertiesSupported and GetProperties. By using these methods, we inform the PropertyGrid that our property is complex (has several nested properties), and return descriptors of these nested properties (we show a single nested boolean property for each bit flag):

public override PropertyDescriptorCollection 
       GetProperties(ITypeDescriptorContext context, 
       object value, Attribute[] attributes)
  Type myType = value.GetType();
  string[] myNames = Enum.GetNames(myType);
  Array myValues = Enum.GetValues(myType);
  if(myNames != null)
    PropertyDescriptorCollection myCollection = 
           new PropertyDescriptorCollection(null);
    for(int i = 0; i < myNames.Length; i++)
        myCollection.Add(new EnumFieldDescriptor(myType, 
                         myNames[i], context));
    return myCollection;

Each bit flag (nested property) is represented with the EnumFieldDescriptor class inherited from the standard .NET class named SimplePropertyDescriptor. This is a base class which allows you to represent a custom property in the PropertyGrid. The main interests of the EnumFieldDescriptor class are the GetValue and SetValue methods. The GetValue method is called each time the PropertyGrid displays a custom property (a bit flag), and returns the value of the custom property (in our case it is a boolean value which indicates whether the bit flag is included in the enumeration property value):

public override object GetValue(object component)
  return ((int)component & (int)Enum.Parse(ComponentType, Name)) != 0;

In the code snippet above, ComponentType is the type of the enumeration, and Name is the name of the enumeration field (bit flag).

The SetValue method is more complicated, it is called when the user modifies a custom property. In our case, it sets a particular bit flag to the enumeration value:

public override void SetValue(object component, object value)
  bool myValue = (bool)value;
  int myNewValue;
    myNewValue = ((int)component) | (int)Enum.Parse(ComponentType, Name);
    myNewValue = ((int)component) & ~(int)Enum.Parse(ComponentType, Name);

  FieldInfo myField = component.GetType().GetField("value__", 
                      BindingFlags.Instance | BindingFlags.Public);
  myField.SetValue(component, myNewValue);
  fContext.PropertyDescriptor.SetValue(fContext.Instance, component);

In this method, we accept a boxed enumeration value (the object type component parameter) which we should modify. The main problem is that an enumeration is a value type (non-reference type) and if we unbox it (convert from the object type to an enumeration type), we will obtain a different object, not the one passed with the component parameter. To solve it, I have done a little trick. An enumeration, in its core, stores its actual value in a private parameter named value__ (you can trace it with a disassembler or decompiler). I have accessed this parameter by using reflection, and set its value without unboxing.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Sergey Gorbenko
Software Developer (Senior)
Canada Canada
No Biography provided

You may also be interested in...

Comments and Discussions

QuestionWhy is it necessary to set the value of "value__" using reflection? Pin
Mizan Rahman15-May-12 2:00
memberMizan Rahman15-May-12 2:00 
AnswerRe: Why is it necessary to set the value of "value__" using reflection? Pin
Sergey Gorbenko21-May-12 16:51
memberSergey Gorbenko21-May-12 16:51 
GeneralDoesn't work Pin
Tim McCurdy6-Jul-07 8:32
memberTim McCurdy6-Jul-07 8:32 
GeneralRe: Doesn't work Pin
Serge Gorbenko30-Jul-07 21:53
memberSerge Gorbenko30-Jul-07 21:53 
GeneralRe: Doesn't work Pin
Tim McCurdy31-Jul-07 17:26
memberTim McCurdy31-Jul-07 17:26 
GeneralUPDATE Pin
SeriousM22-Dec-06 14:55
memberSeriousM22-Dec-06 14:55 
GeneralRe: UPDATE Pin
greenxiar10-Aug-07 20:20
membergreenxiar10-Aug-07 20:20 
GeneralVB.NET Pin
eschneider10023-Oct-06 16:21
membereschneider10023-Oct-06 16:21 
GeneralNice Pin
leppie20-Jun-06 11:26
memberleppie20-Jun-06 11:26 
GeneralExcellent! Pin
Alexey A. Popov20-Jun-06 9:23
memberAlexey A. Popov20-Jun-06 9:23 

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
Web03 | 2.8.151126.1 | Last Updated 20 Jun 2006
Article Copyright 2006 by Sergey Gorbenko
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid