I want to make a ComboBox where the user can type an integer value into the text area, but the drop-down list contains several “defaults” values. For instance, the items in the drop-down list would be formatted like this:
- Default – 0
- Value 1 – 1
- Value 2 – 2
What I want is that when the user selects an item (e.g. “Default – 0”), the ComboBox text will display only the number “0” rather than “Default – 0”. The word “Default” is just informational text.
I have played with the following events: SelectedIndexChanged, SelectedValueChanged, and SelectionChangeCommitted, but I was not able to change the text of the ComboBox.
private void ModificationCombobox_SelectionChangeCommitted(object sender, EventArgs e)
{
ComboBox comboBox = (ComboBox)sender; // That cast must not fail.
if (comboBox.SelectedIndex != -1)
{
comboBox.Text = this.values[comboBox.SelectedItem.ToString()].ToString(); // Text is not updated after...
}
}
You can define a class for your
ComboBoxitem, then create aList<ComboBoxItem>and use it as yourCombobox.DataSource. With this you can setComboBox.DisplayMemberto a property you want displaying and still get reference to your object fromComboBox_SelectedIndexChanged():[edit]
If you want to change displayed text when box toogles between DropDown states try this: (this is a concept, not sure how that would behave)