C#列挙タイプドロップダウン・ボックスにバインド


 
namespace WindowsApplication1 { enum Weakday { , , , , , , } public partial class Form1 : Form { public Form1() { InitializeComponent(); BinderEdays(); } /// <summary> /// /// </summary> private void BinderEdays() { // Weakday Type type = typeof(Weakday); FieldInfo[] fields = type.GetFields(); for(int i=1;i<fields.Length;i++) { this.comboBox1.Items.Add(fields[i].Name); } } } }

共通の方法で、任意の列挙をドロップダウンボックスにバインドできる場合は、以下のコードを使用します.
 public Form1() { InitializeComponent(); BinderEdays<Weakday>(); } //// <summary> ///  T struct , enum /// </summary> private void BinderEdays<T>() where T : struct { Type type = typeof(T); FieldInfo[] fields = type.GetFields(); for(int i=1;i<fields.Length;i++) { this.comboBox1.Items.Add(fields[i].Name); } }