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.End(); } return(Convert.ChangeType(value, type)); }
public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value) { // Validate parameter(s). if (provider == null) { return(value); } if (value == null) { return(value); } // Validate value is a enum type with Flags attribute. if (!value.GetType().IsEnum) { return(value); } object[] attributes = value.GetType().GetCustomAttributes(typeof(FlagsAttribute), false); if ((attributes == null) || (attributes.Length == 0)) { return(value); } // Show panel. IWindowsFormsEditorService service = (IWindowsFormsEditorService)provider.GetService(typeof(IWindowsFormsEditorService)); if (service != null) { FlagsEditorControl control = new FlagsEditorControl(value); service.DropDownControl(control); // Create new enum value. Type type = value.GetType(); object newValue = Activator.CreateInstance(type); FieldInfo field = type.GetFields(BindingFlags.Public | BindingFlags.Instance)[0]; field.SetValue(newValue, control.EditValue); control.Dispose(); return(newValue); } else { return(value); } }
public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value) { // Validate parameter(s). if (provider == null) { return value; } if (value == null) { return value; } // Validate value is a enum type with Flags attribute. if (!value.GetType().IsEnum) { return value; } object[] attributes = value.GetType().GetCustomAttributes(typeof(FlagsAttribute), false); if ((attributes == null) || (attributes.Length == 0)) { return value; } // Show panel. IWindowsFormsEditorService service = (IWindowsFormsEditorService)provider.GetService(typeof(IWindowsFormsEditorService)); if (service != null) { FlagsEditorControl control = new FlagsEditorControl(value); service.DropDownControl(control); // Create new enum value. Type type = value.GetType(); object newValue = Activator.CreateInstance(type); FieldInfo field = type.GetFields(BindingFlags.Public | BindingFlags.Instance)[0]; field.SetValue(newValue, control.EditValue); control.Dispose(); return newValue; } else { return value; } }