350 questions
Edit, let's do this with an enum - full source
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Windows.Forms;
using EnumDescriptions.Classes;
namespace EnumDescriptions
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
List<KeyValuePair<string, Enum>> result =
EnumHelper.GetItemsAsDictionary<WindowKeys>();
comboBox1.DisplayMember = "Key";
comboBox1.ValueMember = "Value";
comboBox1.DataSource = result;
}
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show(comboBox1.SelectedValue.ToString());
}
}
public enum WindowKeys
{
[Description("Windows + M")]
M = 1,
[Description("Windows + A")]
A = 2
}
}
Core code
public class EnumHelper
{
public static List<KeyValuePair<string, Enum>> GetItemsAsDictionary<T>() =>
Enum.GetValues(typeof(T)).Cast<T>()
.Cast<Enum>()
.Select(value => new KeyValuePair<string, Enum>(
(Attribute.GetCustomAttribute(value.GetType().GetField(value.ToString())!,
typeof(DescriptionAttribute)) as DescriptionAttribute)!.Description, value))
.ToList();
}