/// <summary> /// Overrides the method used to provide basic behaviour for selecting editor. /// Shows our custom control for editing the value. /// </summary> /// <param name="context">The context of the editing control</param> /// <param name="provider">A valid service provider</param> /// <param name="value">The current value of the object to edit</param> /// <returns>The new value of the object</returns> public override object EditValue(System.ComponentModel.ITypeDescriptorContext context, System.IServiceProvider provider, object value) { if (context != null && context.Instance != null && provider != null) { edSvc = (System.Windows.Forms.Design.IWindowsFormsEditorService)provider.GetService(typeof(System.Windows.Forms.Design.IWindowsFormsEditorService)); if (edSvc != null) { // Create a CheckedListBox and populate it with all the enum values clb = new System.Windows.Forms.CheckedListBox(); clb.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle; clb.CheckOnClick = true; clb.MouseDown += new System.Windows.Forms.MouseEventHandler(this.OnMouseDown); clb.MouseMove += new System.Windows.Forms.MouseEventHandler(this.OnMouseMoved); tooltipControl = new System.Windows.Forms.ToolTip(); tooltipControl.ShowAlways = true; foreach (string name in System.Enum.GetNames(context.PropertyDescriptor.PropertyType)) { // Get the enum value object enumVal = System.Enum.Parse(context.PropertyDescriptor.PropertyType, name); // Get the int value int intVal = (int)System.Convert.ChangeType(enumVal, typeof(int), System.Globalization.CultureInfo.CurrentCulture); // Get the description attribute for this field System.Reflection.FieldInfo fi = context.PropertyDescriptor.PropertyType.GetField(name); System.ComponentModel.DescriptionAttribute[] attrs = (System.ComponentModel.DescriptionAttribute[])fi.GetCustomAttributes(typeof(System.ComponentModel.DescriptionAttribute), false); // Store the the description string tooltip = attrs.Length > 0 ? attrs[0].Description : string.Empty; // Get the int value of the current enum value (the one being edited) int intEdited = (int)System.Convert.ChangeType(value, typeof(int), System.Globalization.CultureInfo.CurrentCulture); // Creates a clbItem that stores the name, the int value and the tooltip EnumEditorItem item = new EnumEditorItem(enumVal.ToString(), intVal, tooltip); // Get the checkstate from the value being edited //bool checkedItem = (intEdited & intVal) > 0; bool checkedItem = (intEdited & intVal) == intVal; // Add the item with the right check state clb.Items.Add(item, checkedItem); } // Show our CheckedListbox as a DropDownControl. // This methods returns only when the dropdowncontrol is closed edSvc.DropDownControl(clb); // Get the sum of all checked flags int result = 0; foreach (EnumEditorItem obj in clb.CheckedItems) { //result += obj.Value; result |= obj.Value; } // return the right enum value corresponding to the result return System.Enum.ToObject(context.PropertyDescriptor.PropertyType, result); } } return value; }
/// <summary> /// Overrides the method used to provide basic behaviour for selecting editor. /// Shows our custom control for editing the value. /// </summary> /// <param name="context">The context of the editing control</param> /// <param name="provider">A valid service provider</param> /// <param name="value">The current value of the object to edit</param> /// <returns>The new value of the object</returns> public override object EditValue(System.ComponentModel.ITypeDescriptorContext context, System.IServiceProvider provider, object value) { if (context != null && context.Instance != null && provider != null) { edSvc = (System.Windows.Forms.Design.IWindowsFormsEditorService)provider.GetService(typeof(System.Windows.Forms.Design.IWindowsFormsEditorService)); if (edSvc != null) { // Create a CheckedListBox and populate it with all the enum values clb = new System.Windows.Forms.CheckedListBox(); clb.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle; clb.CheckOnClick = true; clb.MouseDown += new System.Windows.Forms.MouseEventHandler(this.OnMouseDown); clb.MouseMove += new System.Windows.Forms.MouseEventHandler(this.OnMouseMoved); tooltipControl = new System.Windows.Forms.ToolTip(); tooltipControl.ShowAlways = true; foreach (string name in System.Enum.GetNames(context.PropertyDescriptor.PropertyType)) { // Get the enum value object enumVal = System.Enum.Parse(context.PropertyDescriptor.PropertyType, name); // Get the int value int intVal = (int)System.Convert.ChangeType(enumVal, typeof(int), System.Globalization.CultureInfo.CurrentCulture); // Get the description attribute for this field System.Reflection.FieldInfo fi = context.PropertyDescriptor.PropertyType.GetField(name); System.ComponentModel.DescriptionAttribute[] attrs = (System.ComponentModel.DescriptionAttribute[])fi.GetCustomAttributes(typeof(System.ComponentModel.DescriptionAttribute), false); // Store the the description string tooltip = attrs.Length > 0 ? attrs[0].Description : string.Empty; // Get the int value of the current enum value (the one being edited) int intEdited = (int)System.Convert.ChangeType(value, typeof(int), System.Globalization.CultureInfo.CurrentCulture); // Creates a clbItem that stores the name, the int value and the tooltip EnumEditorItem item = new EnumEditorItem(enumVal.ToString(), intVal, tooltip); // Get the checkstate from the value being edited //bool checkedItem = (intEdited & intVal) > 0; bool checkedItem = (intEdited & intVal) == intVal; // Add the item with the right check state clb.Items.Add(item, checkedItem); } // Show our CheckedListbox as a DropDownControl. // This methods returns only when the dropdowncontrol is closed edSvc.DropDownControl(clb); // Get the sum of all checked flags int result = 0; foreach (EnumEditorItem obj in clb.CheckedItems) { //result += obj.Value; result |= obj.Value; } // return the right enum value corresponding to the result return(System.Enum.ToObject(context.PropertyDescriptor.PropertyType, result)); } } return(value); }