Chinaunix首页 | 论坛 | 博客
  • 博客访问: 50719
  • 博文数量: 20
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 255
  • 用 户 组: 普通用户
  • 注册时间: 2014-09-19 14:01
文章分类

全部博文(20)

文章存档

2014年(20)

我的朋友

分类: C#/.net

2014-09-20 21:48:51

对于enum类型

使用遍历enum类型的元素并填充combox

 代码如下 复制代码

foreach ( HatchStyle hs1 in Enum.GetValues(typeof(HatchStyle)))
{
    comboBox1.Items.Add(hs1.ToString());
}

例子

 代码如下 复制代码

public enum BeepType
{
SimpleBeep = -1,
IconAsterisk = 0x00000040,
IconExclamation = 0x00000030,
IconHand = 0x00000010,
IconQuestion = 0x00000020,
Ok = 0x00000000,
        }
string[] x = (string[])Enum.GetNames(typeof(Win32Api.BeepType));
int[] y = (int[])Enum.GetValues(typeof(Win32Api.BeepType));
for (int i = 0; i < 100; i++)
{
    MessageBox.Show(i.ToString());
    Win32Api.MessageBeep((Win32Api.BeepType)i);
}

枚举可用来存储字符串与数字的值对,相当于一个对照表
常用方法:GetName(),GetValue(),Parse()

 代码如下 复制代码

using System;
 
 public class EnumTest {
     enum Days { Saturday, Sunday, Monday, Tuesday, Wednesday, Thursday, Friday };
     enum BoiPoints { Celcius = 100, Fahrenheit = 212 };
     [FlagsAttribute]
     enum Colors { Red = 1, Green = 2, Blue = 4, Yellow = 8 };
 
     public static void Main() {
 
         Type weekdays = typeof(Days);
         Type boiling = typeof(BoilingPoints);
 
         Console.WriteLine("The days of the week, and their corresponding values in the Days Enum are:");
 
         foreach ( string s in Enum.GetNames(weekdays) )
             Console.WriteLine( "{0,-11}= {1}", s, Enum.Format( weekdays, Enum.Parse(weekdays, s), "d"));
 
         Console.WriteLine();
         Console.WriteLine("Enums can also be created which have values that represent some meaningful amount.");
         Console.WriteLine("The BoilingPoints Enum defines the following items, and corresponding values:");
 
         foreach ( string s in Enum.GetNames(boiling) )
             Console.WriteLine( "{0,-11}= {1}", s, Enum.Format(boiling, Enum.Parse(boiling, s), "d"));
 
         Colors myColors = Colors.Red | Colors.Blue | Colors.Yellow;
         Console.WriteLine();
         Console.WriteLine("myColors holds a combination of colors. Namely: {0}", myColors);
     }
 }

阅读(746) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~