Katzs

I'm using a combobox as a dropdown menu to choose something, and when I run the following code:

Code Snippet

private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
{
primary = comboBox2.SelectedText;
}


and select something from the list, primary is ending up as an empty string. Why is that


Re: Windows Forms General ComboBoxes again

Katzs

Alright so I just changed the code to:

Code Snippet

private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)

{
primary = comboBox2.Items[comboBox2.SelectedIndex].ToString();
}



and it works now. Could someone explain the difference




Re: Windows Forms General ComboBoxes again

Scotty2012

I always just do something like

Code Snippet

private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)


{

primary = comboBox2.Text;
}




Re: Windows Forms General ComboBoxes again

boban.s

That depends of how you use the ComboBox control. It can work in connected or disconnected mode. If you set DataSource property, then it work connected, and if you fill it manually, then is working in so called 'disconnected' mode. From this will depend what are you reading, Text in disconnected or SelectedText(i would always use SelectedValue because that is what matters) in connected mode.






Re: Windows Forms General ComboBoxes again

BabyFace Jay

Hi, actually "this.comboBox1.SelectedText" gets the current highlighted text inside the combobox

I suppose "this.comboBox1.Text" is the one that you were looking for





Re: Windows Forms General ComboBoxes again

ChunSheng Tang - MSFT


MSDN library wrote:

Property ValueA string that represents the currently selected text in the combo box. If DropDownStyle is set to DropDownList, the return value is an empty string ("").


SelectedText is text of the editable "textbox" portion of the combobox. The selecteditem targets the "listbox" portion of the combobox. In your scenario, the later of your code snippet is precise.

Best Regards
Chunsheng Tang





Re: Windows Forms General ComboBoxes again

Pr.wa'el

Hi . as moderator replayed you , SelectedText is text of the editable "textbox" portion of the combobox. The selecteditem targets the "listbox" portion of the combobox. but i'd like to say that , it depends of how you use the ComboBox control. i mean in connected or disconnected mode. If you uses connected mode by setting DataSource SelectedText will work fine . Else you fill it manually, then is working in so called 'disconnected' mode then " Text " Property will work in this disconnected mode .