I have a ComboBox
control with the DropDownStyle
properties set to DropDownList
.
Once there is an item selected, how can I clear the selection from the ComboBox
without deleting any Items in it ?
I'd normally use something like that:
myComboBox.Text.Clear();
But I can't do that. Any idea how I could clear it ?
This question is related to
c#
winforms
drop-down-menu
combobox
all depend on the configuration. for me works
comboBox.SelectedIndex = -1;
my configuration
DropDownStyle: DropDownList
(text can't be changed for the user)
In c# if you make you comboBox configuration style DropDownList
or DropDown
then use both of them in this method to clear.
ComboBox1.SelectedIndex = -1;
comboBox1.Text = " ";
This is the best and easiest way to set your combo box back to default settings without erasing the contents of the combo box.
Try specifying the actual index of the item you want erase the text from and set it's Text equal to "".
myComboBox[this.SelectedIndex].Text = ""
or
myComboBox.selectedIndex.Text = ""
I don't remember the exact syntax but it's something along those lines.
write the following code:
comboBox1.Items[comboBox1.SelectedIndex] = string.Empty;
The only way I could get it to work:
comboBox1.Text = "";
For some reason ionden's solution didn't work for me.
The following code will work:
ComboBox1.SelectedIndex.Equals(String.Empty);
This is what you need:
comboBox1.ResetText();
nameofcombobox.SelectedItem=-1;
Source: Stackoverflow.com