I have my enumHelper class that contains these:
public static IList GetValues()
{
IList list = new List();
foreach (object val
Take a look at this article. You can do this using the System.ComponentModel.DescriptionAttribute or creating your own attribute:
///
/// Provides a description for an enumerated type.
///
[AttributeUsage(AttributeTargets.Enum | AttributeTargets.Field,
AllowMultiple = false)]
public sealed class EnumDescriptionAttribute : Attribute
{
private string description;
///
/// Gets the description stored in this attribute.
///
/// The description stored in the attribute.
public string Description
{
get
{
return this.description;
}
}
///
/// Initializes a new instance of the
/// class.
///
/// The description to store in this attribute.
///
public EnumDescriptionAttribute(string description)
: base()
{
this.description = description;
}
}
You then need to decorate the enum values with this new attribute:
public enum SimpleEnum
{
[EnumDescription("Today")]
Today,
[EnumDescription("Last 7 days")]
Last7,
[EnumDescription("Last 14 days")]
Last14,
[EnumDescription("Last 30 days")]
Last30,
[EnumDescription("All")]
All
}
All of the "magic" takes place in the following extension methods:
///
/// Provides a static utility object of methods and properties to interact
/// with enumerated types.
///
public static class EnumHelper
{
///
/// Gets the of an
/// type value.
///
/// The type value.
/// A string containing the text of the
/// .
public static string GetDescription(this Enum value)
{
if (value == null)
{
throw new ArgumentNullException("value");
}
string description = value.ToString();
FieldInfo fieldInfo = value.GetType().GetField(description);
EnumDescriptionAttribute[] attributes =
(EnumDescriptionAttribute[])
fieldInfo.GetCustomAttributes(typeof(EnumDescriptionAttribute), false);
if (attributes != null && attributes.Length > 0)
{
description = attributes[0].Description;
}
return description;
}
///
/// Converts the type to an
/// compatible object.
///
/// The type.
/// An containing the enumerated
/// type value and description.
public static IList ToList(this Type type)
{
if (type == null)
{
throw new ArgumentNullException("type");
}
ArrayList list = new ArrayList();
Array enumValues = Enum.GetValues(type);
foreach (Enum value in enumValues)
{
list.Add(new KeyValuePair(value, GetDescription(value)));
}
return list;
}
}
Finally, you can then simply bind the combobox:
combo.DataSource = typeof(SimpleEnum).ToList();