211 lines
		
	
	
		
			5.0 KiB
		
	
	
	
		
			C#
		
	
	
	
	
	
			
		
		
	
	
			211 lines
		
	
	
		
			5.0 KiB
		
	
	
	
		
			C#
		
	
	
	
	
	
using System;
 | 
						|
using System.Collections.Generic;
 | 
						|
using System.ComponentModel;
 | 
						|
using System.Drawing.Design;
 | 
						|
using System.Text;
 | 
						|
using System.Windows.Forms;
 | 
						|
using System.Windows.Forms.Design;
 | 
						|
 | 
						|
namespace DevComponents.Charts.Design
 | 
						|
{
 | 
						|
    #region FlagsEnumUIEditor
 | 
						|
 | 
						|
    public class FlagsEnumUIEditor : UITypeEditor
 | 
						|
    {
 | 
						|
        private FlagsCheckedListBox _FlagCb;
 | 
						|
 | 
						|
        public FlagsEnumUIEditor()
 | 
						|
        {
 | 
						|
            _FlagCb = new FlagsCheckedListBox();
 | 
						|
        }
 | 
						|
 | 
						|
        public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
 | 
						|
        {
 | 
						|
            return (UITypeEditorEditStyle.DropDown);
 | 
						|
        }
 | 
						|
 | 
						|
        public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
 | 
						|
        {
 | 
						|
            if (context != null && context.Instance != null && provider != null)
 | 
						|
            {
 | 
						|
                IWindowsFormsEditorService edSvc =
 | 
						|
                    (IWindowsFormsEditorService)provider.GetService(typeof(IWindowsFormsEditorService));
 | 
						|
 | 
						|
                if (edSvc != null)
 | 
						|
                {
 | 
						|
                    Enum e = (Enum)Convert.ChangeType(value, context.PropertyDescriptor.PropertyType);
 | 
						|
 | 
						|
                    _FlagCb.Value = e;
 | 
						|
                    edSvc.DropDownControl(_FlagCb);
 | 
						|
 | 
						|
                    return (_FlagCb.Value);
 | 
						|
                }
 | 
						|
            }
 | 
						|
 | 
						|
            return (base.EditValue(provider, value));
 | 
						|
        }
 | 
						|
    }
 | 
						|
 | 
						|
    #endregion
 | 
						|
 | 
						|
    public class FlagsCheckedListBox : CheckedListBox
 | 
						|
    {
 | 
						|
        private Type _Type;
 | 
						|
        private Enum _Value;
 | 
						|
        private bool _UpdatingStates;
 | 
						|
 | 
						|
        public FlagsCheckedListBox()
 | 
						|
        {
 | 
						|
            CheckOnClick = true;
 | 
						|
            BorderStyle = BorderStyle.None;
 | 
						|
        }
 | 
						|
 | 
						|
        #region Public properties
 | 
						|
 | 
						|
        #region Value
 | 
						|
 | 
						|
        public Enum Value
 | 
						|
        {
 | 
						|
            get
 | 
						|
            {
 | 
						|
                return (_Value);
 | 
						|
            }
 | 
						|
 | 
						|
            set
 | 
						|
            {
 | 
						|
                _Value = value;
 | 
						|
                _Type = value.GetType();
 | 
						|
 | 
						|
                InitializeCheckedListBox();
 | 
						|
            }
 | 
						|
        }
 | 
						|
 | 
						|
        #endregion
 | 
						|
 | 
						|
        #endregion
 | 
						|
 | 
						|
        #region InitializeCheckedListBox
 | 
						|
 | 
						|
        private void InitializeCheckedListBox()
 | 
						|
        {
 | 
						|
            Items.Clear();
 | 
						|
 | 
						|
            foreach (string name in Enum.GetNames(_Type))
 | 
						|
            {
 | 
						|
                object o = Enum.Parse(_Type, name);
 | 
						|
                uint value = (uint)Convert.ChangeType(o, typeof(uint));
 | 
						|
 | 
						|
                FlagsCheckedListBoxItem item = new FlagsCheckedListBoxItem(value, name);
 | 
						|
 | 
						|
                Items.Add(item);
 | 
						|
            }
 | 
						|
 | 
						|
            SetCheckedItems();
 | 
						|
        }
 | 
						|
 | 
						|
        #region OnItemCheck
 | 
						|
 | 
						|
        protected override void OnItemCheck(ItemCheckEventArgs e)
 | 
						|
        {
 | 
						|
            base.OnItemCheck(e);
 | 
						|
 | 
						|
            if (_UpdatingStates == false)
 | 
						|
            {
 | 
						|
                FlagsCheckedListBoxItem item = Items[e.Index] as FlagsCheckedListBoxItem;
 | 
						|
 | 
						|
                uint sum;
 | 
						|
 | 
						|
                if (e.NewValue == CheckState.Checked &&
 | 
						|
                    (item.Value == 0 || item.Text.Equals("None")))
 | 
						|
                {
 | 
						|
                    sum = item.Value;
 | 
						|
                }
 | 
						|
                else
 | 
						|
                {
 | 
						|
                    sum = CalculateCheckedSum();
 | 
						|
 | 
						|
                    if (e.NewValue == CheckState.Checked)
 | 
						|
                        sum += item.Value;
 | 
						|
                    else
 | 
						|
                        sum -= item.Value;
 | 
						|
                }
 | 
						|
                   
 | 
						|
                _Value = (Enum)(Enum.ToObject(_Type, sum));
 | 
						|
 | 
						|
                SetCheckedItems();
 | 
						|
            }
 | 
						|
        }
 | 
						|
 | 
						|
        #endregion
 | 
						|
 | 
						|
        #region SetCheckedItems
 | 
						|
 | 
						|
        private void SetCheckedItems()
 | 
						|
        {
 | 
						|
            _UpdatingStates = true;
 | 
						|
 | 
						|
            int value = (int)Convert.ChangeType(_Value, typeof(int));
 | 
						|
 | 
						|
            for (int i = 0; i < Items.Count; i++)
 | 
						|
            {
 | 
						|
                FlagsCheckedListBoxItem item = Items[i] as FlagsCheckedListBoxItem;
 | 
						|
 | 
						|
                if (item.Value == 0)
 | 
						|
                    SetItemChecked(i, value == 0);
 | 
						|
 | 
						|
                else if (item.Text.Equals("None"))
 | 
						|
                    SetItemChecked(i, value == item.Value);
 | 
						|
 | 
						|
                else
 | 
						|
                    SetItemChecked(i, ((item.Value & value) == item.Value));
 | 
						|
            }
 | 
						|
 | 
						|
            _UpdatingStates = false;
 | 
						|
        }
 | 
						|
 | 
						|
        #endregion
 | 
						|
 | 
						|
        #endregion
 | 
						|
 | 
						|
        #region CalculateCheckedSum
 | 
						|
 | 
						|
        private uint CalculateCheckedSum()
 | 
						|
        {
 | 
						|
            uint sum = 0;
 | 
						|
 | 
						|
            for (int i = 0; i < Items.Count; i++)
 | 
						|
            {
 | 
						|
                FlagsCheckedListBoxItem item = Items[i] as FlagsCheckedListBoxItem;
 | 
						|
 | 
						|
                if (GetItemChecked(i))
 | 
						|
                    sum |= item.Value;
 | 
						|
            }
 | 
						|
 | 
						|
            return (sum);
 | 
						|
        }
 | 
						|
 | 
						|
        #endregion
 | 
						|
    }
 | 
						|
 | 
						|
    #region FlagsCheckedListBoxItem
 | 
						|
 | 
						|
    public class FlagsCheckedListBoxItem
 | 
						|
    {
 | 
						|
        public uint Value;
 | 
						|
        public string Text;
 | 
						|
 | 
						|
        public FlagsCheckedListBoxItem(uint value, string text)
 | 
						|
        {
 | 
						|
            Value = value;
 | 
						|
            Text = text;
 | 
						|
        }
 | 
						|
 | 
						|
        public override string ToString()
 | 
						|
        {
 | 
						|
            return (Text);
 | 
						|
        }
 | 
						|
    }
 | 
						|
 | 
						|
    #endregion
 | 
						|
}
 |