I'm using a comboBox with an enum of Countries, and I wonder how I get the selected country from the comboBox to a string. Previously I have used cmbCountries.SelectedIndex to get the int number of the selected country, but now I want the text. Help is preciated! Thanks!
2 Answers
rIf you enum has the same values as the combobox, just cast your selected index to a variable of your enum type and then do a ToString() on that. a bit like this:
void Main()
{
int selectedIndex = 1;
Country test;
test = (Country)selectedIndex;
Console.WriteLine(test.ToString());
Console.WriteLine(((Country)selectedIndex).ToString());
}
enum Country
{
None,
Australia,
Austria,
England,
France,
Germany,
UnitedStates
}
Alternatively, you can just get the Text of the combobox.