在C#中,description属性显示枚举的描述和值。

huangapple go评论98阅读模式
英文:

description attribute in c# shows the description and the value from enum

问题

I want it to show only the text that is within the description area, how can I do that? Thanks!

我希望它只显示在描述区域内的文本,我该如何做到?谢谢!

英文:

Im trying to present a description of enum and it presents me the description and the value.
i want it to show only the text that is within the description area, how can i do that? thanks!
this is the enum:
[Description("Choclate Bar")] choclateBar,
[Description("Caffe Protein Bar")] CafeProteinBar,
[Description("Vanilla Cookie")] vanillaCookie,

this is the code im using to present the enums in a combobox
enter image description here
and the second picture is how its shows.
enter image description here

i want it to show in the combobox the values that within the description ("Choclate Bar", ...)

答案1

得分: 1

以下是您要翻译的内容:

以下是一个在ComboBox中呈现枚举的示例,其中第一项用于提示用户进行选择。

枚举

  1. public enum BookCategories
  2. {
  3. [Description("选项")]
  4. Select = 0,
  5. [Description("太空旅行")]
  6. SpaceTravel = 1,
  7. [Description("冒险")]
  8. Adventure = 2,
  9. [Description("流行体育")]
  10. Sports = 3,
  11. [Description("汽车")]
  12. Automobile = 4,
  13. [Description("使用C#编程")]
  14. Programming = 5
  15. }

用于在ComboBox中呈现的模型

  1. public class BookContainer
  2. {
  3. public Enum Name { get; set; }
  4. public string Description { get; set; }
  5. // 用于在ComboBox中显示
  6. public override string ToString() => Description;
  7. }

获取枚举描述的代码

  1. public static class EnumHelper
  2. {
  3. /// <summary>
  4. /// 创建一个KeyValuePair,其中Key是描述,Value是枚举值
  5. /// </summary>
  6. /// <typeparam name="T">要处理的枚举</typeparam>
  7. public static List<KeyValuePair<string, Enum>> GetItemsAsDictionary<T>() =>
  8. Enum.GetValues(typeof(T)).Cast<T>()
  9. .Cast<Enum>()
  10. .Select(value => new KeyValuePair<string, Enum>(
  11. (Attribute.GetCustomAttribute(value.GetType()
  12. .GetField(value.ToString())!,
  13. typeof(DescriptionAttribute)) as DescriptionAttribute)!
  14. .Description, value))
  15. .ToList();
  16. public static List<BookContainer> Container<T>()
  17. {
  18. List<KeyValuePair<string, Enum>> data = GetItemsAsDictionary<T>();
  19. return data.Select(category
  20. => new BookContainer() { Name = category.Value, Description = category.Key })
  21. .ToList();
  22. }
  23. }

表单代码,一个ComboBox,两个按钮

  1. public partial class Form1 : Form
  2. {
  3. public Form1()
  4. {
  5. InitializeComponent();
  6. Shown += OnShown;
  7. }
  8. private void OnShown(object? sender, EventArgs e)
  9. {
  10. categoriesComboBox.DataSource = EnumHelper.Container<BookCategories>();
  11. }
  12. private void CurrentButton_Click(object sender, EventArgs e)
  13. {
  14. BookContainer container = (categoriesComboBox.SelectedItem as BookContainer)!;
  15. if (Equals(container.Name, BookCategories.Select))
  16. {
  17. MessageBox.Show("请选择一个项目");
  18. }
  19. else
  20. {
  21. MessageBox.Show($"选择是 {container.Name}");
  22. }
  23. }
  24. private void SetCurrentButton_Click(object sender, EventArgs e)
  25. {
  26. // 这里是如何设置需要的选定索引
  27. categoriesComboBox.SelectedIndex =
  28. categoriesComboBox.FindString(BookCategories.Adventure.ToString());
  29. }
  30. }

在C#中,description属性显示枚举的描述和值。

英文:

The following presents an Enum in a ComboBox where the first item is to prompt the user for a selection.

Enum

  1. public enum BookCategories
  2. {
  3. [Description(&quot;Options&quot;)]
  4. Select = 0,
  5. [Description(&quot;Space Travel&quot;)]
  6. SpaceTravel = 1,
  7. [Description(&quot;Adventure&quot;)]
  8. Adventure = 2,
  9. [Description(&quot;Popular sports&quot;)]
  10. Sports = 3,
  11. [Description(&quot;Cars&quot;)]
  12. Automobile = 4,
  13. [Description(&quot;Programming with C#&quot;)]
  14. Programming = 5
  15. }

Model for presenting in a ComboBox

  1. public class BookContainer
  2. {
  3. public Enum Name { get; set; }
  4. public string Description { get; set; }
  5. // for displaying in ComboBox
  6. public override string ToString() =&gt; Description;
  7. }

Code to get descriptions for the Enum

  1. public static class EnumHelper
  2. {
  3. /// &lt;summary&gt;
  4. /// Create a &lt;see cref=&quot;KeyValuePair&quot;/&gt; where Key is the description,
  5. /// Value the enum value
  6. /// &lt;/summary&gt;
  7. /// &lt;typeparam name=&quot;T&quot;&gt;enum to work with&lt;/typeparam&gt;
  8. public static List&lt;KeyValuePair&lt;string, Enum&gt;&gt; GetItemsAsDictionary&lt;T&gt;() =&gt;
  9. Enum.GetValues(typeof(T)).Cast&lt;T&gt;()
  10. .Cast&lt;Enum&gt;()
  11. .Select(value =&gt; new KeyValuePair&lt;string, Enum&gt;(
  12. (Attribute.GetCustomAttribute(value.GetType()
  13. .GetField(value.ToString())!,
  14. typeof(DescriptionAttribute)) as DescriptionAttribute)
  15. !.Description, value))
  16. .ToList();
  17. public static List&lt;BookContainer&gt; Container&lt;T&gt;()
  18. {
  19. List&lt;KeyValuePair&lt;string, Enum&gt;&gt; data = GetItemsAsDictionary&lt;T&gt;();
  20. return data.Select(category
  21. =&gt; new BookContainer() { Name = category.Value, Description = category.Key })
  22. .ToList();
  23. }
  24. }

Form code, a ComboBox, two buttons

  1. public partial class Form1 : Form
  2. {
  3. public Form1()
  4. {
  5. InitializeComponent();
  6. Shown += OnShown;
  7. }
  8. private void OnShown(object? sender, EventArgs e)
  9. {
  10. categoriesComboBox.DataSource = EnumHelper.Container&lt;BookCategories&gt;();
  11. }
  12. private void CurrentButton_Click(object sender, EventArgs e)
  13. {
  14. BookContainer container = (categoriesComboBox.SelectedItem as BookContainer)!;
  15. if (Equals(container.Name, BookCategories.Select))
  16. {
  17. MessageBox.Show(&quot;Please make a selection&quot;);
  18. }
  19. else
  20. {
  21. MessageBox.Show($&quot;Selection is {container.Name}&quot;);
  22. }
  23. }
  24. private void SetCurrentButton_Click(object sender, EventArgs e)
  25. {
  26. // here is how to set the selected index if needed
  27. categoriesComboBox.SelectedIndex =
  28. categoriesComboBox.FindString(BookCategories.Adventure.ToString());
  29. }
  30. }

在C#中,description属性显示枚举的描述和值。

huangapple
  • 本文由 发表于 2023年5月29日 23:44:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/76358677.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定