I am currently developing a C # application.
I need to use an enumeration with a combo box to get the selected month. I have the following to create an enumeration:
enum Months { January = 1, February, March, April, May, June, July, August, September, October, November, December };
Then I initialize the combo box using the following:
cboMonthFrom.Items.AddRange(Enum.GetNames(typeof(Months)));
This bit of code works fine, however the problem is that I am trying to get the selected enumeration value for the selected month
To get the value of an enumerator from a combo box, I used the following:
private void cboMonthFrom_SelectedIndexChanged(object sender, EventArgs) { Months selectedMonth = (Months)cboMonthFrom.SelectedItem; Console.WriteLine("Selected Month: " + (int)selectedMonth); }
However, when I try to run the above code, I get the error "The first random exception of type System.InvalidCastException .
What I did wrong.
Thanks for any help you can provide.
Boardy
source share