I have a ComboBox on a form. The DropDownStyle
property of the ComboBox is set to DropDown
, so that the user can select an item from the drop down
You can try this:
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (comboBox1.SelectedIndex > -1)
{
string value = comboBox1.Items[comboBox1.SelectedIndex].ToString().Substring(4);
this.BeginInvoke((MethodInvoker)delegate { this.comboBox1.Text = value; });
}
}
Just to clarify, are you displaing "101 Cat", "102 Dog", etc. when the user dropdown the combo and displaying them when selected? Is 101 the key for the "Cat", 102 the key for the "Dog", and so on? If so, why are you displaying them and not only displaying the text of each item (if this is your requirement, apologize my answer). For what I understand, I'll configure the combobox valuemember to the property that returns you the 101, 102, etc. and the displaymember to the property that returns you the text of each items. This way, you'll already get what you want, i.e. displaying the "Cat", "Dog" and "Bird" text. Also, you can attach an event to the TextChanged event of the combo if you want furter processing.