73

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.

1

18 Answers 18

138

Did you try cboxHour.Items.Clear()?

4
  • 2
    That's the winner. I'll mark you as the chosen answer when I can.
    – Fuzz Evans
    Feb 17, 2012 at 1:57
  • 16
    If your comboBox is data bounded then set comboBoxName.DataSource = null before Clearing Oct 19, 2016 at 13:40
  • This is likely insufficient. Calling ResetText() as answered by @beanmf below is likely also required.
    – David Carr
    Nov 16, 2017 at 20:18
  • In case of 'DataSource' property of a combobox used, we have to assign null as follows : modeComboBox.DataSource = null; (modeComboBox.DataSource = new BindingSource(dict, null);) Aug 8, 2019 at 5:44
92

If you just want to clear the current selection, but leave all of the items in the list, you can use:

cboxHour.SelectedIndex = -1
1
  • 4
    When I employ this, the selectedIndexChanged event fires. In SelectedIndexChanged event, I return out of the eventif SelectedIndex is -1.
    – Jim Lahman
    Dec 17, 2013 at 20:27
25

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();
1
  • 1
    Agreed on above. I was having the same problem in VS2013, where the 4 calls Items.Clear(), SelectedIndex=-1, SelectedText=string.empty, and SelectedText=string.empty was insufficient. Items.Clear() followed by ResetText() did the trick.
    – David Carr
    Nov 16, 2017 at 20:16
16

You can use

Cbo.Items.Clear();

or

Cbo.DataSource = null;

if you have a binding on it.

0
8

Answer for your question is:

metroComboBox1.SelectedItem = null;
anycomboBox1.SelectedItem=null;
3
  • 1
    combox.items.clear() deletes the items from the list while setting the selected item to null clears the selected data. excellent. thanks.
    – kakkarot
    Jan 7, 2016 at 8:52
  • 1
    if you are having trouble setting the index to -1, try setting the selecteditem to null or nothing. This worked for me.
    – Rob
    Mar 16, 2018 at 16:36
  • This is the real answer. It also solves an issue I had with .ResetText - then, I wasn't able any more to programmatically re-load a selection. But .SelectedItem = null does the trick. Excellent! Thank you!
    – Matt
    Oct 7, 2019 at 17:00
5
cboxHour.Items.Clear();

this works

1
  • 1
    Can you expand on your answer a little? It's great if you can provide a little bit of context to your answer. May 28, 2013 at 23:02
5

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();
4

If there is value binding part for your combobox. Use below code to clear its value:

cboxHour.SetSelectedIndex(-1);
2

Use:

comboBox1.ResetText();

and it's done.

Docs: ComboBox.ResetText Method (Namespace: System.Windows.Forms) https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.combobox.resettext?view=netframework-4.8

1
  • While this code may solve the question, including an explanation of how and why this solves the problem would really help to improve the quality of your post, and probably result in more up-votes. Remember that you are answering the question for readers in the future, not just the person asking now. Please edit your answer to add explanations and give an indication of what limitations and assumptions apply. Mar 28, 2019 at 18:27
1

Mine worked with:

ComboBox.removeAllItems();

If it doesn't read that well its, remove all items.

0
0

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();
0
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");
}
0

In WPF You can try this code

cbHours.Items.Clear();

0

You can try the below option for clearing the selected text and all items from the ComboBox.

comboBox1.SelectedIndex = -1;
comboBox1.Items.Clear();
0

This worked for me when I added ComboBox.Focus()

ComboBox.Items.Clear();
ComboBox.ResetText();
ComboBox.Focus();
0

> Its work for me:

    ComboCapacity.DataSource = null;
    ComboCapacity.Items.Clear();
    ComboCapacity.ResetText();
                    
0

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;
-1

I have just changed the text of the combobox, like this:

Combobox.Text = "Select...";
1
  • 1
    This changes what is in the text portion and not clear the combo box as requested. Your response assumes that "Select..." was the default in there. You also don't say after you did it whether it solved the problem
    – Fabulous
    Jul 11, 2017 at 23:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.