How Do I Clear a Combobox?
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...
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.
18 Answers
Did you try cboxHour.Items.Clear()?
If you just want to clear the current selection, but leave all of the items in the list, you can use:
cboxHour.SelectedIndex = -1
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();
You can use
Cbo.Items.Clear();
or
Cbo.DataSource = null;
if you have a binding on it.
Answer for your question is:
metroComboBox1.SelectedItem = null;
anycomboBox1.SelectedItem=null;
cboxHour.Items.Clear();
this works
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();
If there is value binding part for your combobox. Use below code to clear its value:
cboxHour.SetSelectedIndex(-1);
Use:
comboBox1.ResetText();
and it's done.
Docs: ComboBox.ResetText Method (Namespace: System.Windows.Forms)
Mine worked with:
ComboBox.removeAllItems();
If it doesn't read that well its, remove all items.
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();
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");
}
In WPF You can try this code
cbHours.Items.Clear();
You can try the below option for clearing the selected text and all items from the ComboBox.
comboBox1.SelectedIndex = -1;
comboBox1.Items.Clear();
This worked for me when I added ComboBox.Focus()
ComboBox.Items.Clear();
ComboBox.ResetText();
ComboBox.Focus();
> Its work for me:
ComboCapacity.DataSource = null;
ComboCapacity.Items.Clear();
ComboCapacity.ResetText();
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;
I have just changed the text of the combobox, like this:
Combobox.Text = "Select...";