I have some combo-boxes that are set up as drop down lists, and the user can pick a number in them. I also have a Clear button that should clear the text from the combo boxe
Use:
comboBox1.ResetText();
and it's done.
Docs: ComboBox.ResetText Method (Namespace: System.Windows.Forms) https://docs.microsoft.com/en-us/dotnet/api/system.windows.forms.combobox.resettext?view=netframework-4.8
private void Resetbtn_Click(object sender, EventArgs e)
{
comboBox1.Items.Clear(); // it will clear a combobox
comboBox1.Items.Add("Student"); //then add combobox elements again.
comboBox1.Items.Add("Staff");
}
Combo Box, DropDown all are having the same logic to clear/remove all items from them and it is like below.
//For checkbox list
cblTest.Items.Clear();
//For drop down list
ddlTest.Items.Clear();
When ComboBox
is not data-bound, I've found I need both: Clear()
removes the items but still leaves the SelectedItem
's text, while ResetText()
removes that text. VS2008.
ComboBox.Items.Clear();
ComboBox.ResetText();
If you just want to clear the current selection, but leave all of the items in the list, you can use:
cboHour.SelectedIndex = -1
This worked for me when I added ComboBox.Focus()
ComboBox.Items.Clear();
ComboBox.ResetText();
ComboBox.Focus();