Exemplo n.º 1
0
        /// <summary>
        /// 设置ComboBox枚举选中项
        /// </summary>
        /// <typeparam name="T">枚举</typeparam>
        /// <param name="combox">ComboBox</param>
        /// <param name="enumValue">选中项值</param>
        public static void SetEnumToComboBox <T>(System.Windows.Forms.ComboBox combox, T enumValue) where T : struct
        {
            Type enumType = typeof(T);

            EnumEx.AssertEnum(enumType);
            if (combox == null)
            {
                throw new ArgumentNullException(nameof(combox), "目标控件不能为null");
            }

            if (combox.Items.Count == 0)
            {
                return;
            }

            try
            {
                for (int i = 0; i < combox.Items.Count; i++)
                {
                    if (object.Equals(enumValue, ((FieldInfoEx)combox.Items[i]).Value))
                    {
                        combox.SelectedIndex = i;
                        return;
                    }
                }

                throw new Exception(string.Format("枚举类型:{0}与绑定到ComboBox的枚举类型不匹配", enumType.FullName));
            }
            catch (Exception ex)
            {
                throw new Exception("设定值失败", ex);
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// 绑定枚举值到ComboBox
        /// </summary>
        /// <typeparam name="T">枚举</typeparam>
        /// <param name="combox">ComboBox</param>
        /// <param name="defaultSelectedValue">默认选中项值</param>
        /// <param name="ignoreList">忽略项列表</param>
        public static void BindingEnumToComboBox <T>(System.Windows.Forms.ComboBox combox, T defaultSelectedValue, IEnumerable <T> ignoreList = null) where T : struct
        {
            Type enumType = typeof(T);

            EnumEx.AssertEnum(enumType);
            if (combox == null)
            {
                throw new ArgumentNullException(nameof(combox), "目标控件不能为null");
            }

            if (ignoreList == null)
            {
                ignoreList = new List <T>();
            }

            try
            {
                combox.Items.Clear();
                List <FieldInfoEx> items = EnumEx.GetEnumFieldInfoExList(enumType);
                if (items.Count == 0)
                {
                    return;
                }

                int selectedIndex = -1;
                for (int i = 0; i < items.Count; i++)
                {
                    var  item     = items[i];
                    bool isIgnore = (from ignoreItem in ignoreList where object.Equals(item.Value, ignoreItem) select ignoreItem).Count() > 0;
                    if (isIgnore)
                    {
                        continue;
                    }

                    if (object.Equals(item.Value, defaultSelectedValue))
                    {
                        selectedIndex = i;
                    }

                    combox.Items.Add(item);
                }

                combox.DisplayMember = nameof(FieldInfoEx.DisplayName);
                combox.SelectedIndex = selectedIndex == -1 ? 0 : selectedIndex;
            }
            catch (Exception ex)
            {
                throw new Exception("绑定值失败", ex);
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// 获取ComboBox枚举选中项
        /// </summary>
        /// <typeparam name="T">枚举类型</typeparam>
        /// <param name="combox">ComboBox</param>
        /// <returns>选中项枚举值</returns>
        public static T GetEnumFromComboBox <T>(System.Windows.Forms.ComboBox combox) where T : struct
        {
            EnumEx.AssertEnum <T>();
            if (combox == null)
            {
                throw new ArgumentNullException(nameof(combox), "目标控件不能为null");
            }

            try
            {
                FieldInfoEx selectedItem = (FieldInfoEx)(combox.Items[combox.SelectedIndex]);
                return((T)selectedItem.Value);
            }
            catch (Exception ex)
            {
                throw new Exception("获取值失败", ex);
            }
        }