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;
        }
    }
}

調用方式:spa

System.Collections.Hashtable arr = Comm.EnumHelper.EnumToHashtable(typeof(tImageFormat));
            foreach (string item in arr.Values)
                cb.Items.Add(item);

其中的cb表示ComboBox對象,你能夠替換成你的下拉框對象。code

相關文章
相關標籤/搜索