Bit Flags Type Converter

Introduction

Hi, 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 property grid 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 own TypeConverter that would help to edit bit fields properties.

There can be a few solutions to this problem; for example, you 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
{
   ...
   [TypeConverter(typeof(FlagsEnumConverter))]
   public FontStyle FontStyle
   {
      get
      {
         ...
      }
      set
      {
         ...
      }
   }
   ...
}

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

The 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)
   {
      ...
   }
}

TypeConverter has several overridable properties and methods that allow you to customize the appearance of a property in the property grid. In this type converter, I overrode two methods: GetPropertiesSupported and GetProperties. By using these methods, you inform the property grid that your property is complex (has several nested properties) and returns descriptors of these nested properties (you 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 that allows you to represent a custom property in a property grid. The main interest of the EnumFieldDescriptor class are the GetValue and SetValue methods. The GetValue method is called each time the property grid displays a custom property (a bit flag) and returns the value of the custom property (in this case, it is a Boolean value that 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 this 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;
   if(myValue)
    myNewValue = ((int)component) | (int)Enum.Parse(ComponentType,
                                                    Name);
   else
      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, you accept a boxed enumeration value (the object type component parameter) that you should modify. The main problem is that an enumeration is value type (non-reference type) and if you unbox it (convert from the object type to an enumeration type), you will obtain a different object, not the one passed with the component parameter. To solve it, I have done a little trick. Enumeration in the 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.



Downloads

Comments

  • There are no comments yet. Be the first to comment!

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • With 81% of employees using their phones at work, companies have stopped asking: "Is corporate data leaking from personal devices?" and started asking: "How do we effectively prevent corporate data from leaking from personal devices?" The answer has not been simple. ZixOne raises the bar on BYOD security by not allowing email data to reside on the device. In addition, Zix allows employees to maintain complete control of their personal device, therefore satisfying privacy demands of valued employees and the …

  • If you need new tools and tricks to make your meetings profitable and productive, then 5 Tips in 5 Minutes: A Quick Guide for More Profitable Sales Meetings is for you. Timely, practical tips that you can incorporate in just seconds will save you literally hours in travel and meeting time, not to mention help you to focus on what your sales prospects really want to know and how you can meet their needs. Get 5in5: A Quick Guide for More Profitable Sales Meetings and start building your sales the smarter, faster …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds