Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ComboBox.SelectedText doesn't give me the SelectedText

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 "

like image 623
Cocoa Dev Avatar asked Apr 17 '12 15:04

Cocoa Dev


People also ask

How can I find my ComboBox ID?

to get the ID: int itemID = AssetTypeComboBox. SelectedIndex + 1; //returns the ID number in the DB.

What is the selectedtext property in combobox?

Gets or sets the text that is selected in the editable portion of a ComboBox. A string that represents the currently selected text in the combo box. If DropDownStyle is set to DropDownList, the return value is an empty string (""). You can use the SelectedText property to retrieve or change the currently selected text in a ComboBox control.

How do I change the selected text in a combobox?

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.

How do I get the selectedtext value of a selected item?

When the user selects an item from the drop-down list or by using the UP ARROW and DOWN ARROW keys, the text for the new item is automatically selected. However, if you try to get the SelectedText value in a SelectedIndexChanged or SelectedValueChanged event handler, the property returns an empty string.

What is the “selectedtext” property?

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".


2 Answers

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.

like image 105
Marco Avatar answered Oct 20 '22 04:10

Marco


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.

like image 21
4b0 Avatar answered Oct 20 '22 05:10

4b0