views:

68

answers:

4

Hi,

I use VS2008 C# + Windows Forms. I can't understand why comboBox does not behave the way it should. In Design mode, I added a comboBox to my form, and edit Items to add "A" and "B". Double-clicking brings me to SelectedIndexChanged event, which I edit to display the selected text with MessageBox.

private void comboBoxImageSet_SelectedIndexChanged(object sender, EventArgs e)
{
    MessageBox.Show(comboBoxImageSet.SelectedText);
}

When I run, and select "A" or "B" in the comboBox, the MessageBox appears, but nothing is written. Why?

Thanks.

A: 

SelectedText:

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

That is, it gets the text that is currently marked.

You want to use SelectedItem.ToString().

Charles
+2  A: 

The SelectedText property returns the text that is marked in the combobox, not the selected item. If the combobox is editable you can mark a part of the text and the SelectedText property will return the marked text. Look here.

What you are interested in is the SelectedItem property or the SelectedValue property.

Rune Grimstad
+1  A: 

ComboBox.SelectedText

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

Use SelectedItem instead of SelectedText

volody
+1  A: 

Here the differences between the selection properties of a ComboBox control.

  1. SelectedIndex;
  2. SelectedItem;
  3. SelectedText;
  4. SelectedValue.

The SelectedIndex property :

Gets or sets the index specifying the currently selected item.

Simply indicates the index of the selected item in the selection list. (Information provided for your kind information only. =))


The SelectedItem property :

Gets or sets currently selected item in the ComboBox.

The SelectedItem represents the element that is currently selected as per the ListControl of the ComboBox. That is why this is what you want to use, to answer your question.


The SelectedText property :

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

That is, when you edit the TextBox portion of the ComboBox, the text that might be selected when you enter for edit, or any other type of text selection. This indeed does include any selection made through the ListControl portion of the ComboBox. For instance, if your ComboBox.DropDownStyle property is set to ComboBoxStyle.DropDownList, then you will never be able to select any text in the editable portion of the ComboBox. Despite, you're able to select another item within the its list. That is why it is not the right property to use to serve your purpose.


The SelectedValue property :

Gets or sets the value of the member property specified by the ValueMember property.

Only used when using DataBinding, in conjunction with the DisplayMember property. For instance, when you want to display the name of a customer, and select him by his database Id, then the DisplayMember should display the customer's name, and the ValueMember the Id. This way, when you select one customer, the SelectedValue changes and raises the SelectedValueChanged event inherited from the ListControl. (Information provided for your kind information only. =))


Will Marcouiller