EnumHelper枚举常用操作类
在项目中需要把枚举填充到下拉框中,所以使用统一的方法实现,测试代码如下:
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对象,你可以替换成你的下拉框对象。
关注我】。(●'◡'●)
如果,您希望更容易地发现我的新博客,不妨点击一下绿色通道的【因为,我的写作热情也离不开您的肯定与支持,感谢您的阅读,我是【Jack_孟】!
本文来自博客园,作者:jack_Meng,转载请注明原文链接:https://www.cnblogs.com/mq0036/p/6084262.html
【免责声明】本文来自源于网络,如涉及版权或侵权问题,请及时联系我们,我们将第一时间删除或更改!
posted on 2016-11-21 09:07 jack_Meng 阅读(1416) 评论(0) 编辑 收藏 举报