/// <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(ITypeDescriptorContext context, IServiceProvider provider, object value)
        {
            if (context != null
              && context.Instance != null
              && provider != null)
              {
            edSvc = (IWindowsFormsEditorService)provider.GetService(typeof (IWindowsFormsEditorService));

            if (edSvc != null)
            {
              // Create a CheckedListBox and populate it with all the enum values
              clb = new CheckedListBox();
              clb.BorderStyle = BorderStyle.FixedSingle;
              clb.CheckOnClick = true;
              clb.MouseDown += OnMouseDown;
              clb.MouseMove += OnMouseMoved;

              tooltipControl = new ToolTip();
              tooltipControl.ShowAlways = true;

              foreach (string name in Enum.GetNames(context.PropertyDescriptor.PropertyType))
              {
            // Get the enum value
            object enumVal = Enum.Parse(context.PropertyDescriptor.PropertyType, name);
            // Get the int value
            int intVal = (int)Convert.ChangeType(enumVal, typeof (int));

            // Get the description attribute for this field
            FieldInfo fi = context.PropertyDescriptor.PropertyType.GetField(name);
            DescriptionAttribute[] attrs =
              (DescriptionAttribute[])fi.GetCustomAttributes(typeof (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)Convert.ChangeType(value, typeof (int));

            // Creates a clbItem that stores the name, the int value and the tooltip
            clbItem item = new clbItem(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 (clbItem obj in clb.CheckedItems)
              {
            //result += obj.Value;
            result |= obj.Value;
              }

              // return the right enum value corresponding to the result
              return Enum.ToObject(context.PropertyDescriptor.PropertyType, result);
            }
              }

              return value;
        }
Example #2
0
        /// <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(ITypeDescriptorContext context, IServiceProvider provider, object value)
        {
            if (context != null &&
                context.Instance != null &&
                provider != null)
            {
                edSvc = (IWindowsFormsEditorService)provider.GetService(typeof(IWindowsFormsEditorService));

                if (edSvc != null)
                {
                    // Create a CheckedListBox and populate it with all the enum values
                    clb              = new CheckedListBox();
                    clb.BorderStyle  = BorderStyle.FixedSingle;
                    clb.CheckOnClick = true;
                    clb.MouseDown   += new MouseEventHandler(this.OnMouseDown);
                    clb.MouseMove   += new MouseEventHandler(this.OnMouseMoved);

                    tooltipControl            = new ToolTip();
                    tooltipControl.ShowAlways = true;

                    foreach (string name in Enum.GetNames(context.PropertyDescriptor.PropertyType))
                    {
                        // Get the enum value
                        object enumVal = Enum.Parse(context.PropertyDescriptor.PropertyType, name);
                        // Get the int value
                        int intVal = (int)Convert.ChangeType(enumVal, typeof(int));

                        // Get the description attribute for this field
                        System.Reflection.FieldInfo fi    = context.PropertyDescriptor.PropertyType.GetField(name);
                        DescriptionAttribute[]      attrs = (DescriptionAttribute[])fi.GetCustomAttributes(typeof(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)Convert.ChangeType(value, typeof(int));

                        // Creates a clbItem that stores the name, the int value and the tooltip
                        clbItem item = new clbItem(enumVal.ToString(), intVal, tooltip);

                        // Get the checkstate from the value being edited
                        bool checkedItem = (intEdited & intVal) > 0;

                        // 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 (clbItem obj in clb.CheckedItems)
                    {
                        result += obj.Value;
                    }

                    // return the right enum value corresponding to the result
                    return(Enum.ToObject(context.PropertyDescriptor.PropertyType, result));
                }
            }

            return(value);
        }