How do I clear a combobox?
C#WinformsComboboxC# Problem Overview
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 boxes but I can't seem to get it. I've tried:
//doesn't work
cboxHour.Text = "";
and
//doesn't work
cboxHour.ResetText();
This seems like it should be so straight forward but I'm just not getting it.
C# Solutions
Solution 1 - C#
Did you try cboxHour.Items.Clear()
?
Solution 2 - C#
If you just want to clear the current selection, but leave all of the items in the list, you can use:
cboxHour.SelectedIndex = -1
Solution 3 - C#
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();
Solution 4 - C#
You can use
Cbo.Items.Clear();
or
Cbo.DataSource = null;
if you have a binding on it.
Solution 5 - C#
Answer for your question is:
metroComboBox1.SelectedItem = null;
anycomboBox1.SelectedItem=null;
Solution 6 - C#
cboxHour.Items.Clear();
this works
Solution 7 - C#
If you have applied datasource to combobox, then it will not be cleared as cmb.Items.Clear()
.
For that you have to assign datasource null
to combobox.
cmb.DataSource = null;
cmb.Items.Clear();
Solution 8 - C#
If there is value binding part for your combobox. Use below code to clear its value:
cboxHour.SetSelectedIndex(-1);
Solution 9 - C#
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
Solution 10 - C#
Mine worked with:
ComboBox.removeAllItems();
If it doesn't read that well its, remove all items.
Solution 11 - C#
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();
Solution 12 - C#
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");
}
Solution 13 - C#
In WPF You can try this code
cbHours.Items.Clear();
Solution 14 - C#
You can try the below option for clearing the selected text and all items from the ComboBox.
comboBox1.SelectedIndex = -1;
comboBox1.Items.Clear();
Solution 15 - C#
This worked for me when I added ComboBox.Focus()
ComboBox.Items.Clear();
ComboBox.ResetText();
ComboBox.Focus();
Solution 16 - C#
> Its work for me:
ComboCapacity.DataSource = null;
ComboCapacity.Items.Clear();
ComboCapacity.ResetText();
Solution 17 - C#
My Soluce to Clear DropDownList ComboBox C# VS2022
ComboBox _ComboBox = new ComboBox();
_ComboBox.DropDownStyle = ComboBoxStyle.DropDownList;
_ComboBox.DataSource = DataTablexxx;
_ComboBox.DisplayMember = "xxxx";
_ComboBox.ValueMember = "Idn";
//Enable the form now
this.Visible = true;
this.ResumeLayout(false);
this.PerformLayout();
//Select the item by ident saved
_ComboBox.SelectedValue = Properties.Settings.Default.Idn;
On Event Button :
_ComboBox.SelectedIndex = -1;
_ComboBox.SelectedItem = null;
Solution 18 - C#
I have just changed the text of the combobox, like this:
Combobox.Text = "Select...";