//在公共类中定义枚举
using System;
using System.Collections.Generic;
using System.Text;
namespace WindowsFormsApplication
{
public class UseEnum//定义成Public类型,则可以在当前项目中进行使用
{
//定义一个枚举类型
//枚举中的内容都是自己定义的
public enum TimeofDay
{
Morning,
Afternoon,
Evening
}
}
}
//在Form页面中使用枚举的示例代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication
{
public partial class FormUseEnum : Form
{
public FormUseEnum()
{
InitializeComponent();
}
//使用枚举的方法
private void useEnum_Click(object sender, EventArgs e)
{
WindowsFormsApplication.UseEnum.TimeofDay myEnum = UseEnum.TimeofDay.Afternoon;//拿到枚举中的值
MessageBox.Show(myEnum.ToString());
}
}
}