在项目中需要把枚举填充到下拉框中,所以使用统一的方法实现,测试代码如下,需要的朋友可以参考下
在项目中需要把枚举填充到下拉框中,所以使用统一的方法实现,测试代码如下,需要的朋友可以参考下
在项目中需要把枚举填充到下拉框中,所以使用统一的方法实现,测试代码如下,需要的朋友可以参考下
测试代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | 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; } }} |
调用方式:
1 2 3 | System.Collections.Hashtable arr = Comm.EnumHelper.EnumToHashtable(typeof(tImageFormat)); foreach (string item in arr.Values) cb.Items.Add(item); |
其中的cb表示ComboBox对象,你可以替换成你的下拉框对象。
发表评论