January 15, 2007

Use enumartor as data source (combobox)

[C#]


enum WorkDays{
Monday=1,
Tuesday=2,
Wednesday=3,
Thursday=4,
Friday=5
}

System.Windows.Forms.ComboBox comboBox1;

//init combobox object
comboBox1 = new System.Windows.Forms.ComboBox();

//assign data source
comboBox1.DataSource = Enum.GetValues(typeof(WorkDays));


In the SelectedValueChanged event for the ComboBox.
[C#]

private void ComboBox1ValueChanged(object sender, EventArgs e)
{
System.Windows.Forms.ComboBox cmb = (System.Windows.Forms.ComboBox)sender;
WorkDays SelectedWDayEnum = (WorkDays)cmb.SelectedValue;
string SelectedWDayText = SelectedWDayEnum.ToString();
int SelectedWDayValue = (Int32)SelectedWDayText;
}


Source extended:
http://www.syncfusion.com/FAQ/WindowsForms/FAQ_c88c.aspx#q1124q

No comments: