Moslem7026
Moslem7026

Reputation: 3338

ComboBox Not Returned the selectedItem

i have bounded DataSource to a combox box and then set this Code

Combox1.ValueMember = "CapacityID";
Combox1.DisplayMember = "Capacitys";

it show Data with no problem , but when i want gain selectedtext it returned me "" and using selectedItem , return name of combo box ! selectedvalue return correct data .

Combox1.SelectedItem.ToString(); //return "Combox1"
Combox1.SelectedValue.ToString(); //Work Correctly
Combox1.SelectedText.ToString(); // return ""

Upvotes: 1

Views: 4110

Answers (4)

Moslem7026
Moslem7026

Reputation: 3338

ComboBox.Text.Tostring() returned selected text and solved my problem

 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.

Upvotes: 1

Pranay Rana
Pranay Rana

Reputation: 176896

Combox1.SelectedItem retunrs you selected ListItem object not text value of selected item

its should be like :

ListItem li = Combox1.SelectedItem;

or

Object selectedItem = comboBox1.SelectedItem;

    MessageBox.Show("Selected Item Text: " + selectedItem.ToString() );

From MSDN : http://msdn.microsoft.com/en-us/library/system.windows.forms.combobox.selecteditem.aspx


Combox1.SelectedText - Check Msdn for this : http://msdn.microsoft.com/en-us/library/system.windows.forms.combobox.selectedtext.aspx

FROM MSdn why its return empty string - 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.

Upvotes: 1

Freeman
Freeman

Reputation: 5801

So altough, your question is not really gramatically clear, to get the calue of your selected item always use

Combox1.SelectedValue

Why?

Because:

Combox1.SelectedItem

returns 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 ("").

Upvotes: 0

yogi
yogi

Reputation: 19591

Use

Combox1.SelectedItem.Text // To get SelectedText
Combox1.SelectedItem.Value // To get SelectedValue

Instead of

Combox1.SelectedItem.ToString()

Upvotes: 0

Related Questions