在下拉列表框中绑定枚举
我们曾经见过在PropertyGrid中能自动用下拉列表列出我们自定义的枚举属性。这个自动列出枚举值的功能其实很简单。下面是模仿PropertyGrid实现一个遍历所有枚举绑定到下拉列表的功能:
假设:你定义了这样的一个枚举:
public enum ExceptionCategories
{
/// <summary>
/// Web界面异常
/// </summary>
WebUI = 1,
/// <summary>
/// 业务组件异常
/// </summary>
Business,
/// <summary>
/// 通用业务组件异常
/// </summary>
Commom,
/// <summary>
/// 引擎异常
/// </summary>
Engine,
/// <summary>
/// 文件组件异常
/// </summary>
Files,
/// <summary>
/// 接口业务处理异常
/// </summary>
InterFaseBusiness,
/// <summary>
/// 数据库异常
/// </summary>
DataBase,
/// <summary>
/// 进程级异常
/// </summary>
Global
}或者,你偷懒,定义了这样的一个中文名称的枚举:
public enum ENUM_DEP_TYPE
{
/// <summary>
/// 未设置
/// </summary>
未设置 = 0,
/// <summary>
/// 建设单位,操作工程建设模块
/// </summary>
建设单位 = 1,
/// <summary>
/// 施工单位,操作施工协作模块
/// </summary>
施工单位 = 2,
/// <summary>
/// 监理单位,操作监理协作模块
/// </summary>
监理单位 = 3,
/// <summary>
/// 设计单位,操作设计协作模块
/// </summary>
设计单位 = 4,
/// <summary>
/// 维护单位,操作工程建设模块
/// </summary>
维护单位 = 5
}然后,你想在Winform或Webform界面上面放一个下拉列表框,把这些枚举给列出来让用户选择。最简单的方法就是在下拉列表中绑定我们已经建立好的枚举。实现如下:
string[] MyArray = Enum.GetNames(typeof(ENUM_DEP_TYPE));
cboDepartmentType.DataSource = MyArray; //cboDepartmentType是界面上下拉列表的名称由下拉列表选中的项转换成枚举值方法如下:
if (cboDepartmentType.SelectedIndex >= 0)
{
string strDepType = cboDepartmentType.Text;
ENUM_DEP_TYPE DepType = (ENUM_DEP_TYPE)Enum.Parse(typeof(ENUM_DEP_TYPE), strDepType);
}有了上面方法,你是不是可以实现系统的枚举 比如字体、颜色等绑定到你的列表中呢?
假设:你定义了这样的一个枚举:
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)