c# ComboBox绑定枚举

原:https://www.cnblogs.com/lyh523329053/p/8418719.html

 

 

定义枚举

c# ComboBox绑定枚举
        public enum UserLevel
        {          
            Commmon = 0,
            Administrator,
            Developer
        }
c# ComboBox绑定枚举

方法一

c# ComboBox绑定枚举
        private void Method1()
        {
            comboBox1.DataSource = System.Enum.GetNames(typeof(UserLevel));
            comboBox1.SelectedIndex = 
             comboBox1.FindString(UserLevel.Commmon.ToString()); }
c# ComboBox绑定枚举

方法二

c# ComboBox绑定枚举
        private void Method2()
        {
            foreach (var v in typeof(UserLevel).GetFields())
            {
                if (v.FieldType.IsEnum == true)
                {
                    this.comboBox2.Items.Add(v.Name);
                }
            }
            this.comboBox2.SelectedIndex = 1;
        }
c# ComboBox绑定枚举

取值

            
            UserLevel testenum = (UserLevel)Enum.Parse(typeof(UserLevel), comboBox1.SelectedItem.ToString(), false);
        

 

c# ComboBox绑定枚举

上一篇:Windows下安装Docker并使用TF Serving的pb模型


下一篇:C# 关于winform 嵌入资源文件