ComboBox.SelectedText doesn't give me the SelectedText

C#WinformsStringCombobox

C# Problem Overview


I am building a String and the code looks like

String status = "The status of my combobox is " + comboBoxTest.SelectedText

I am using WinForm in VS2010

The result looks like

>"The status of my combobox is "

C# Solutions


Solution 1 - C#

I think you want to use

String status = "The status of my combobox is " + comboBoxTest.Text

SelectedText property from MSDN

> Gets or sets the text that is selected in the editable portion of a > ComboBox.

while Text property from MSDN

> Gets or sets the text associated with this control.

Solution 2 - C#

From the documentation:

>You can use the SelectedText property to retrieve or change the currently selected text in a ComboBox control. However, you should be aware that the selection can change automatically because of user interaction. For example, if you retrieve the SelectedText value in a button Click event handler, the value will be an empty string. This is because the selection is automatically cleared when the input focus moves from the combo box to the button. > >When the combo box loses focus, the selection point moves to the beginning of the text and any selected text becomes unselected. In this case, getting the SelectedText property retrieves an empty string, and setting the SelectedText property adds the specified value to the beginning of the text.

Solution 3 - C#

I face this problem 5 minutes before.

I think that a solution (with visual studio 2005) is:

myString = comboBoxTest.GetItemText(comboBoxTest.SelectedItem);

Forgive me if I am wrong.

Solution 4 - C#

I think you dont need SelectedText but you may need

String status = "The status of my combobox is " + comboBoxTest.Text;

Solution 5 - C#

To get selected item, you have to use SELECTEDITEM property of comboBox. And since this is an Object, if you wanna assign it to a string, you have to convert it to string, by using ToString() method:

string myItem = comboBox1.SelectedItem.ToString(); //this does the trick

Solution 6 - C#

Try this:

String status = "The status of my combobox is " + comboBoxTest.text;

Solution 7 - C#

Here's how I would approach the problem, assuming you want to change the text of say, a label

    private void comboBoxtest_SelectedIndexChanged(object sender, EventArgs e)
    {
        var combotext = comboBoxtest.Text;
        var status = "The status of my combo box is" + combotext;
        label1.Text = status;
    }

Solution 8 - C#

If you bind your Combobox to something like KeyValuePair, with properties in the constructor like so...:

 DataSource = dataSource,
 DisplayMember = "Value",
 ValueMember = "Key"

so dataSource is of type KeyValuePair...

You end up with having to do this...

  string v = ((KeyValuePair)((ComboBox)c).SelectedItem).Value;

(I had a Dynamic form - where c was of type Control - so had to cast it to ComboBox)

Solution 9 - C#

All of the previous answers explain what the OP 'should' do. I am explaining what the .SelectedText property is.

The .SelectedText property is not the text in the combobox. It is the text that is highlighted. It is the same as .SelectedText property for a textbox.

The following picture shows that the .SelectedText property would be equal to "ort".

enter image description here

Solution 10 - C#

If you just want to know the text in the ComboBox with the editable text box (or the ComboBoxStyle.DropDown style) you can use this:

string str = comboBox.SelectedItem != null ?
                      comboBox.GetItemText(comboBox.SelectedItem) : comboBox.Text;

Solution 11 - C#

or try this code

 String status = "The status of my combobox is " + comboBoxTest.SelectedItem.ToString();

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionCocoa DevView Question on Stackoverflow
Solution 1 - C#MarcoView Answer on Stackoverflow
Solution 2 - C#4b0View Answer on Stackoverflow
Solution 3 - C#José Antonio López CanoView Answer on Stackoverflow
Solution 4 - C#MarshalView Answer on Stackoverflow
Solution 5 - C#Mitja BoncaView Answer on Stackoverflow
Solution 6 - C#infredhaView Answer on Stackoverflow
Solution 7 - C#Allison SterankoView Answer on Stackoverflow
Solution 8 - C#JGFMKView Answer on Stackoverflow
Solution 9 - C#NathanView Answer on Stackoverflow
Solution 10 - C#c00000fdView Answer on Stackoverflow
Solution 11 - C#dansasu11View Answer on Stackoverflow