2013-05-27 109 views
0

我有我已經宣佈枚舉如下命名空間枚舉名稱:自定義組合框

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"。 我該怎麼辦?

+0

需要使用'替換'mmethod – Rahul

+0

獲取字符串,然後將其添加到ComboBox1.String str = BaudRate.BR5700;如果(str.contains(「BR」))str = str.replace(「BR」,「」);希望它可以幫助你。 – MahaSwetha

+0

@MahaSwetha謝謝! :) –

回答

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"; 
+0

+1:比其他答案中使用的黑客要好得多,這是特定於此枚舉的。 –

+0

@newStackExchangeInstance同意(很喜歡這個答案),但是OP在另一篇文章中提到他無法修改枚舉的代碼。 –

+0

@newStackExchangeInstance:如果我沒有desctiption標籤怎麼辦? 是否仍然可以顯示沒有BR的值? –

2

例如用與string.replace:

BaudRate.BR115200.ToString().Replace("BR",""); 

實例與子:

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

謝謝你!有用。 :) –

+0

沒問題,你應該考慮徹底刪除BR,因爲你似乎不需要它!另外,請標記爲答案! :) – Ted

+0

其實是一個受保護的文件。我無法對其進行更改。 –

2

刪除從枚舉名BR似乎是行動的最合理的做法。鑑於你的枚舉本身被命名爲BaudRate,BR是多餘的。並且,假設它每的值存在於上,它不會爲值名稱添加任何描述性權力。並且,如果枚舉值總是以枚舉名稱爲前綴,則結果將始終清除(BaudRate.9600而不是BaudRate.BR9600)。

如果你不能/不想這樣做,那麼你需要在添加前爲每個值運行一個BaudRate.XXX.ToString().Substring(2)以刪除前兩個字符。

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","")); 
} 
0

有你的組合框定義一樣,如下圖所示:

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

a nd綁定你的枚舉到組合框