Marek Buchtela
Marek Buchtela

Reputation: 1003

Get combobox set value as string

Found several ways to do so, but none of them works for me.. I have a combobox with variable number of options (taken from dynamic xml file). For next, I need to know what the user choosed, and I cant find out how to do it. This is one of ways I found and tried:

string myString = myCombobox.SelectedValue.ToString();

At least I dont get an error, but when I try to show that string, it does nothing.

Upvotes: 4

Views: 38937

Answers (6)

Andrej Lucansky
Andrej Lucansky

Reputation: 735

myCombobox.SelectedItem.ToString(); 

should do the trick for you.

Upvotes: 5

bigcakes
bigcakes

Reputation: 216

string myString = myCombobox.Text;

Upvotes: 3

mangatinanda
mangatinanda

Reputation: 754

Had a similar problem, try this:

string myString = ((ComboBoxItem)myCombobox.SelectedItem).Content.ToString();

It works for me!

Just try to understand seeing myCombobox design.

Upvotes: 6

PasinduM
PasinduM

Reputation: 47

Use mycombobox.SelectedItem.ToString(); Instead of the SelectedValue

Upvotes: 1

Vlad
Vlad

Reputation: 878

try myCombobox.SelectedItem.ToString();

it will work if you are adding items to the combobox using this way:

  comboBox1.Items.Add("Item");

e.g.

        myComboBox.Items.Add("Item1");
        myComboBox.Items.Add("Item2");
        myComboBox.Items.Add("Item3");

        myComboBox.SelectedIndex = 1; //force change selection
        Console.WriteLine(myComboBox.SelectedItem.ToString()); //will output Item2

Upvotes: 0

adripanico
adripanico

Reputation: 1048

Try with myCombobox.SelectedValue.Value.ToString() or myCombobox.SelectedValue.Text.ToString(). Anyway, this question is too low quality for this forum. You need to do some research before asking. That's why I'm voting down you.

Upvotes: 0

Related Questions