Populate ComboBox with DayOfWeek enum?
-
Good day I am using Visual Studio 2010 to develop a Windows app. I am busy with a module where the user can specify setting for days of the week, for example Mondays the lunch duration is 1 hour, Tuesday it is 30 minutes, and so on. I want to display the days of the week (Sunday, Monday, …) in a ComboBox so that the user can select a day. The DisplayMember must be the name, e.g Monday The ValueMember must be the ordinal number of the day in the DayOfWeek enum, e.g. 1 How do I populate the ComboBox using the DayOfWeek enumerator? I tried:
private void Form1_Load(object sender, EventArgs e)
{
for (int j = 0; j < 7; j++)
{
comboDay.Items.Add((DayOfWeek)j);
}
}private void comboDay_SelectedIndexChanged(object sender, EventArgs e)
{
MessageBox.Show("Selected Day = " + comboDay.SelectedValue);
}This shows the DayOfWeek names in the combobox, but the SelectedValue is always null. Any suggestions on better ways of doing this? Thanks. Kobus
-
Good day I am using Visual Studio 2010 to develop a Windows app. I am busy with a module where the user can specify setting for days of the week, for example Mondays the lunch duration is 1 hour, Tuesday it is 30 minutes, and so on. I want to display the days of the week (Sunday, Monday, …) in a ComboBox so that the user can select a day. The DisplayMember must be the name, e.g Monday The ValueMember must be the ordinal number of the day in the DayOfWeek enum, e.g. 1 How do I populate the ComboBox using the DayOfWeek enumerator? I tried:
private void Form1_Load(object sender, EventArgs e)
{
for (int j = 0; j < 7; j++)
{
comboDay.Items.Add((DayOfWeek)j);
}
}private void comboDay_SelectedIndexChanged(object sender, EventArgs e)
{
MessageBox.Show("Selected Day = " + comboDay.SelectedValue);
}This shows the DayOfWeek names in the combobox, but the SelectedValue is always null. Any suggestions on better ways of doing this? Thanks. Kobus
Use
SelectedItem
instead ofSelectedValue
.*pre-emptive celebratory nipple tassle jiggle* - Sean Ewington
"Mind bleach! Send me mind bleach!" - Nagy Vilmos
My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility
-
Use
SelectedItem
instead ofSelectedValue
.*pre-emptive celebratory nipple tassle jiggle* - Sean Ewington
"Mind bleach! Send me mind bleach!" - Nagy Vilmos
My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility
-
It's always the way. As soon as someone else looks at it, you see it.
*pre-emptive celebratory nipple tassle jiggle* - Sean Ewington
"Mind bleach! Send me mind bleach!" - Nagy Vilmos
My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility