14
votes

I am trying to deselect (blank out) a number of combo-boxes in my windows forms application. In my application I have a Reset method that sets the SelectedIndex for each combo to -1. All of my combo-boxes are databound, i.e. each combo-box is populated using a datasource.

I have noticed that sometimes my Reset method works, i.e. it deselects the currently selected item and blanks the combo. However, other times it chooses the first item (SelectedIndex = 0) straight after I attempt to set it to -1. From a users point of view this looks like a bug as it doesn't always "clear" the form.

According to MSDN:

"To deselect the currently selected item, set the SelectedIndex to -1. You cannot set the SelectedIndex of a ComboBox item to -1 if the item is a data-bound item."

Does anyone know of a work around?

Many thanks

14
This is an old thread, but since I was just trying to figure this out and found this, I'll add it here. This behavior is a bug according to MS: support.microsoft.com/en-us/help/327244/…CuppM

14 Answers

26
votes

Use combination of the void and property

comboBox.ResetText();

 //to reset selected value
comboBox.SelectedIndex = -1;
8
votes

Don't know if anyone is still interested in this, seeing as it's now 5 years later, but I found a very easy workaround. Totally non-intuitive (I only found it by looking at the reference source code), but trivial to implement:

ComboBox1.FormattingEnabled = True;

Yep, that's all there is to it!

If you're curious, you can peruse the source code to see what's going on. It appears that the root cause of the bug noted by @CuppM is the attempt to set the position in the data source:

if (!FormattingEnabled || SelectedIndex != -1) {
    this.DataManager.Position = this.SelectedIndex;
} 

I would guess that it should have simply been '&&' instead of '||' in the condition, as the code probably shouldn't be setting the Position to an invalid value regardless of the FormattingEnabled property.

In any case, it allows for a simple workaround. And since the default behavior if the 'Format' property is blank is a no-op, you don't have to change anything else. It just works. :-)

(I should note that I have only tried this with .NET 4.7, so I can't say whether it works for prior versions of the .NET Framework.)

3
votes

You can try to set the Selected Value or Item to null (Nothing in VB)

I cant remember the behavior of throwing an exception. However, I do remember that I used to insert a value called -1, (None) to the combo-box after it was databounded usually through the databind events. I'd recommend get the data in a List and insert the new value to this list. Bind the combo to the List now.

1
votes

Only the following code works for me, so try:

comboBox.ResetText();   //framework  4.0 
1
votes

ComboBox1.SelectedItem = null;

1
votes

For anyone still looking at this old post I wanted to add a note from what Hisham answer.
Make sure to clear your list after inserting his code.

comboBox.ResetText();

//to reset selected value
comboBox.SelectedIndex = -1;
comboBox.Items.Clear();
0
votes

Try assigning null or String.Empty to the SelectedValue property.

0
votes

If your target framework is 4.0 - here is the solution:

Install .Net Framework 4.5 (do not change target framework of your project, just install the framework). After installing, that line deselects databound combobox:

combobox.SelectedValue = 0;

My value member is "Id" int primary key auto-increment, so that field does not contain value 0. However, that won't work on Windows versions, that do not support .net45

0
votes

Try to set the [ComboBoxObj].SelectedIndex=-1; which will make it to empty value. -1 refers to deselect or nullify the value of combobox

Thanks

0
votes

I have had this problem for a while, but if you use:

'ComboBox.ResetText();'

it will make the text "" and leave the items in the combo box unaffected.

i used the following code in my application

 private void UpdateComboBox(ComboBox Box, string Group, List<string> Numbers)
        {
              Box.Items.Clear();
              Box.BeginUpdate();            
              Box.Items.Add("<<Add Contact>>");
              foreach (string item in Numbers)
              {
                   if(item != "")
                        Box.Items.Add(item);
              }
          Box.EndUpdate();
          Box.ResetText();
        }

So i run the method last, once all items are in the combo Box.

0
votes

Try this line of code:

combobox1.Items.clear();

It works for me.

-1
votes

Add to your combobox one empty item, something like this:

cb.Items.Add("");

After this you can deselect your combobox by selecting the last cb item:

cb.SelectedIndex = cb.Items.Count - 1;

There you go!

You'll have the last place empty in your combobox, but it wont bother you. will it? :-)

-1
votes

I got the following error:

There is no row at position 0

when I was setting ComboBox.SelectedItem to -1.

Replacing by ComboBox.ResetText() worked OK. This was using .Net 4.6.1, with VS 2013 where TextFormatting = True by default for ComboBoxes.

-3
votes

you may try to use this solution..

dataGrid.DataSource = Nothing

dataGrid.DataBind()

hope its help!..:D