.net miner

something about life, me and .net
  • Home
  • Categories
    • C#
    • .Net
    • Tools
    • General
    • Visual Studio
    • Open Source Projects
  • Contact Me
.Net•C#•General•Visual Studio

How To Permit Multiple Selections For Enum Properties

28 August 2008 by Ozcan Degirmenci No Comments
Enum - Flags Editor Example Interface Image

An example screenshot of the Flags Editor

PropertyGrid will use default editor for the properties which’s type is enum. This default editor does not allow multiple selections even the Enum has Flags attribute.

I have been prepared a new Editor for this kind of properties.

Our editor’s code is as follows;

internal class FlagsEditor : UITypeEditor
{
   FlagsEditorControl editor = null;
   public FlagsEditor()
   { }
    
   // our editor is a Drop-down editor
   public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
   {
      return UITypeEditorEditStyle.DropDown;
   } 
  
   public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
   {
       // if value is not an enum than we can not edit it
      if  (!(value is Enum))
         throw new Exception("Value doesn't support");
     
      // try to figure out that is this a Flags enum or not ?
      Type enumType = value.GetType();
      object[] attributes =  enumType.GetCustomAttributes(typeof(FlagsAttribute), true);
      if (attributes.Length == 0)
         throw new Exception("Editing enum hasn't got Flags attribute");
      // check the underlying type
      Type type = Enum.GetUnderlyingType(value.GetType());
      if (type != typeof(byte) && type != typeof(sbyte)  && 
            type != typeof(short) && type != typeof(ushort) && 
           type != typeof(int)&& type != typeof(uint))
        return value;
      if (provider != null)
      {
         // use windows forms editor service to show drop down
         IWindowsFormsEditorService edSvc = provider.GetService( 
              typeof(IWindowsFormsEditorService)) as IWindowsFormsEditorService;
         if (edSvc == null)
             return value;
         if (editor == null)
             editor = new FlagsEditorControl(this);
         // prepare list
         editor.Begin(edSvc, value);
         // show drop down now
         edSvc.DropDownControl(editor);
         // now we take the result
         value = editor.Value;
         // reset editor
         editor.End();
      }
      return Convert.ChangeType(value, type);
   }
}

In this code FlagsEditorControl is a UserControl which PropertyGrid hosts it in a drop down during the edit operation.
You can change your design by changing this Control.

To use this editor for a property, we have to write Editor Attribute to that property as the below;

// set editor of this property to our FlagsEditor
[Editor(typeof(FlagsEditor), typeof(UITypeEditor))]
public FileAttributes FlagsAttribute
{
       get { return _FlagsAttribute; }
       set { _FlagsAttribute = value; }
}
Share:
Reading time: 1 min

Search

Projects

  • Process Viewer
  • B-Msn
  • Runtime Member Proxies
  • Searchable RichTextBox

Categories

  • .Net (19)
  • C# (12)
  • General (13)
  • Open Source Projects (4)
  • Quantum (1)
  • Tools (9)
  • Visual Studio (6)

Archives

  • November 2018
  • September 2018
  • September 2010
  • August 2010
  • August 2009
  • September 2008
  • August 2008
  • June 2008
  • May 2008
  • April 2008
  • February 2008
  • January 2008

About me

Hi, my name is Ozcan Degirmenci. I'm co-founder and CEO at Crs Soft.

Tags

Assembly Collection Editor Dialog Support Dynamic method enum Flag Editor Loader Exceptions Ole Calls Open source project open source projects Perfornance Comparison process viewer Proxy quantum Reflection Run Apps From Web schrödinger's cat Searchable Richtextbox Spoon thought experiment thread

© 2018 copyright ÖZCAN DEĞİRMENCİ // All rights reserved.