1

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 2

1

If your combo box is data bound, then just use the index directly against the data.

If not, you can get the item from the control:

cmbCountries.SelectedItem

But if you want just the text value of the selected entry:

cmbCountries.Text

Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.