How to permit multiple selections for Enum properties?

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. You can download the source codes from here

Download Source Codes


Our editor’s code is as follows;
internal class FlagsEditor : UITypeEditor
{
       FlagsEditorControl editor = null;

       public FlagsEditor()
       { }

       // our editor is a DropDown 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; }
}

Related posts

Add comment


 

  Country flag

[b][/b] - [i][/i] - [u][/u]- [quote][/quote]



Live preview

January 7. 2009 10:44 AM


Xenocode Postbuild is the powerful, reliable, and easy-to-use code protection and deployment solution for .NET developers.

Search

Calendar

<<  January 2009  >>
MonTueWedThuFriSatSun
2930311234
567891011
12131415161718
19202122232425
2627282930311
2345678
View posts in large calendar

Disclaimer

© 2007 - 2008
Ozcan DEGIRMENCI
All rights reserved. The content can be used elsewhere given that the source is properly acknowledged.