c# EnumHelper枚举常用操作类
更新时间:2016年11月30日 22:52:28 投稿:mdxy-dxy
在项目中需要把枚举填充到下拉框中,所以使用统一的方法实现,测试代码如下,需要的朋友可以参考下
测试代码如下:
namespace CutPictureTest.Comm
{
public class EnumHelper
{
public static System.Collections.ArrayList GetName(Type enumType)
{
System.Collections.ArrayList arr = new System.Collections.ArrayList();
string[] n = System.Enum.GetNames(enumType);
foreach (string item in n)
arr.Add(item);
return arr;
}
public static T ToEnum<T>(string strEnum)
{
T t = (T)Enum.Parse(typeof(T), strEnum);
return t;
}
public static System.Collections.Hashtable EnumToHashtable(Type enumType)
{
System.Collections.Hashtable ht = new System.Collections.Hashtable();
Array arr = System.Enum.GetValues(enumType);
for (int i = 0; i < arr.Length; i++)
ht.Add(Convert.ToInt16(arr.GetValue(i)), arr.GetValue(i).ToString());
return ht;
}
}
}
调用方式:
System.Collections.Hashtable arr = Comm.EnumHelper.EnumToHashtable(typeof(tImageFormat));
foreach (string item in arr.Values)
cb.Items.Add(item);
其中的cb表示ComboBox对象,你可以替换成你的下拉框对象。
相关文章
C#手动操作DataGridView使用各种数据源填充表格实例
本文主要介绍了C#手动操作DataGridView使用各种数据源填充表格实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧2023-02-02
C# Windows API应用之基于FlashWindowEx实现窗口闪烁的方法
这篇文章主要介绍了C# Windows API应用之基于FlashWindowEx实现窗口闪烁的方法,结合实例形式分析了Windows API函数FlashWindowEx的功能、定义及实现窗口闪烁的相关技巧,需要的朋友可以参考下2016-08-08


最新评论