phadaphunk
phadaphunk

Reputation: 13313

Clear ComboBox selected text

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 ?

Upvotes: 36

Views: 150018

Answers (11)

Ami Schreiber
Ami Schreiber

Reputation: 377

Try specifying the actual index of the item you want erase the text from and set its 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.

Upvotes: -2

user4798349
user4798349

Reputation: 1

comboBox1.Text = " ";

This is the best and easiest way to set your comboBox back to default settings without erasing the contents of the comboBox.

Upvotes: -1

Naqeeb
Naqeeb

Reputation: 1431

nameofcombobox.SelectedItem = -1;

Upvotes: 3

user14743092
user14743092

Reputation:

Try this, if you populate your Combobox with objects:

Combobox.SelectedItem = null;

Upvotes: 0

Codecopath
Codecopath

Reputation: 111

This is what you need:

comboBox1.ResetText();

Upvotes: 10

user11040190
user11040190

Reputation: 1

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;

Upvotes: -1

Justin Dean
Justin Dean

Reputation: 1

The following code will work:

ComboBox1.SelectedIndex.Equals(String.Empty);

Upvotes: -3

Mario Cocozza
Mario Cocozza

Reputation: 39

all depend on the configuration. for me works

comboBox.SelectedIndex = -1;

my configuration

DropDownStyle: DropDownList

(text can't be changed for the user)

Upvotes: 3

Hussein Qadri
Hussein Qadri

Reputation: 1

write the following code:

comboBox1.Items[comboBox1.SelectedIndex] = string.Empty;

Upvotes: -2

Darrell Lloyd Harvey
Darrell Lloyd Harvey

Reputation: 472

The only way I could get it to work:

comboBox1.Text = "";

For some reason ionden's solution didn't work for me.

Upvotes: 24

ionden
ionden

Reputation: 12776

You could change SelectedIndex property:

comboBox1.SelectedIndex = -1;

Upvotes: 85

Related Questions