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 "
to get the ID: int itemID = AssetTypeComboBox. SelectedIndex + 1; //returns the ID number in the DB.
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.
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.
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.
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".
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With