自定义组合框的枚举名称

时间:2013-05-27 07:03:15

标签: c# combobox enums

我有一个命名空间,我在其中声明了枚举如下:

namespace IXMSoft.Business.SDK.Data
{
using System;

public enum BaudRate
{
    BR115200 = 7,
    BR19200 = 4,
    BR230400 = 8,
    BR2400 = 1,
    BR38400 = 5,
    BR4800 = 2,
    BR57600 = 6,
    BR9600 = 3
  }
}

当我使用语句

在另一个命名空间的组合框中检索这些值时
comboBox1.Items.Add(BaudRate.BR5700);

它显示的值例如

  

“BR5700”

我想remove BR in front and just want to display the value as "5700"。 我该怎么办?

5 个答案:

答案 0 :(得分:4)

使用DescriptionAttributeappropriate extension method来阅读它。

public enum BaudRate
{
    [Description("115200 kb")]
    BR115200 = 7,
    [Description("19200 kb")]
    BR19200 = 4,
    [Description("230400 kb")]
    BR230400 = 8,
    [Description("2400 kb")]
    BR2400 = 1,
    [Description("115200 kb")]
    BR38400 = 5,
    [Description("4800 kb")]
    BR4800 = 2,
    [Description("57600 kb")]
    BR57600 = 6,
    [Description("9600 kb")]
    BR9600 = 3
}

扩展方法:

public static class EnumExtension
{
    /// <summary>
    /// Gets the string of an DescriptionAttribute of an Enum.
    /// </summary>
    /// <param name="value">The Enum value for which the description is needed.</param>
    /// <returns>If a DescriptionAttribute is set it return the content of it.
    /// Otherwise just the raw name as string.</returns>
    public static string Description(this Enum value)
    {
        if (value == null)
        {
            throw new ArgumentNullException("value");
        }

        string description = value.ToString();
        FieldInfo fieldInfo = value.GetType().GetField(description);
        DescriptionAttribute[] attributes =
           (DescriptionAttribute[])
         fieldInfo.GetCustomAttributes(typeof(DescriptionAttribute), false);

        if (attributes != null && attributes.Length > 0)
        {
            description = attributes[0].Description;
        }

        return description;
    }

    /// <summary>
    /// Creates an List with all keys and values of a given Enum class
    /// </summary>
    /// <typeparam name="T">Must be derived from class Enum!</typeparam>
    /// <returns>A list of KeyValuePair&lt;Enum, string&gt; with all available
    /// names and values of the given Enum.</returns>
    public static IList<KeyValuePair<Enum, string>> ToList<T>() where T : struct
    {
        var type = typeof(T);

        if (!type.IsEnum)
        {
            throw new ArgumentException("T must be an enum");
        }

        return (IList<KeyValuePair<Enum, string>>)
                Enum.GetValues(type)
                    .OfType<Enum>()
                    .Select(e => new KeyValuePair<Enum, string>(e, e.Description()))
                    .ToArray();
    }

    public static T GetValueFromDescription<T>(string description) where T : struct
    {
        var type = typeof(T);

        if(!type.IsEnum)
        {
            throw new ArgumentException("T must be an enum");
        }

        foreach(var field in type.GetFields())
        {
            var attribute = Attribute.GetCustomAttribute(field,
                typeof(DescriptionAttribute)) as DescriptionAttribute;

            if(attribute != null)
            {
                if(attribute.Description == description)
                {
                    return (T)field.GetValue(null);
                }
            }
            else
            {
                if(field.Name == description)
                {
                    return (T)field.GetValue(null);
                }
            }
        }

        throw new ArgumentOutOfRangeException("description");
        // or return default(T);
    }
}

在,你可以通过调用:

将它应用到你的组合框
var list = EnumExtension.ToList<BaudRate>();
myComboBox.DataSource = list;
myComboBox.ValueMember = "Key";
myComboBox.DisplayMember = "Value";

答案 1 :(得分:2)

string.replace:

的示例
BaudRate.BR115200.ToString().Replace("BR","");

子字符串示例:

BaudRate.BR115200.ToString().Substring(2);

答案 2 :(得分:2)

从枚举名称中删除BR似乎是最合乎逻辑的行动方式。鉴于您的枚举本身名为BaudRate,BR是多余的。并且鉴于它存在于每个值上,它不会为值名称添加任何描述性功能。鉴于枚举值始终以枚举名称为前缀,结果将始终清晰(BaudRate.9600而不是BaudRate.BR9600)。

如果您不能/不想这样做,那么您需要在添加之前对每个值运行BaudRate.XXX.ToString().Substring(2),以便删除前两个字符。

答案 3 :(得分:1)

public enum BaudRate
{
    BR115200 = 7,
    BR19200 = 4,
    BR230400 = 8,
    BR2400 = 1,
    BR38400 = 5,
    BR4800 = 2,
    BR57600 = 6,
    BR9600 = 3
  }
}

foreach (string name in Enum.GetNames(BaudRate))
{
    cmbEnum.Items.Add(name.Replace("BR",""));
}

答案 4 :(得分:0)

让你的组合框定义如下:

<combobox>
   <ComboBoxItem>--</ComboBoxItem>
   <ComboBoxItem>2400</ComboBoxItem>
   <ComboBoxItem>4800</ComboBoxItem>
   <ComboBoxItem>9600</ComboBoxItem>
   <ComboBoxItem>19200</ComboBoxItem> // and soo on
</combobox>

并将您的Enum绑定到组合框

相关问题