• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 454
  • Last Modified:

ListBox.ClearSelected() doesn't work

Whether my .SelectionMode is "One" or "MultiSimple", ClearSelected() selects the first item in the listbox.  Setting .SelectedIndex=-1 has the same effect.  

Any ideas how I can make no items selected without reinstantiating the whole damn control?

Here's my code (not that it really matters):

private System.Windows.Forms.ListBox lstExisting;
lstExisting.ClearSelected();          //doesn't work!!!
1 Solution
are you sure you don't have any code else where that is changing the listbox?  Do you have any events on the list box?  I've tested this, and it does not behave the way you're saying.  
HarmnKardnAuthor Commented:
Yes, I have a _SelectedIndexChanged event on the listbox, but all that's done in the event is enable/disable some command buttons.  So, I commented out the code in the event handler just to be safe, and the problem still remains.
best thing to try, is start with a fresh project and just do it with the minimal bare bones.

If it still doesn't work, then there is a problem with your .net installation.

Otherwise, you've got to determine the differene between your current project and the cut-down dummy version you've just written.

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

HarmnKardnAuthor Commented:
It works if I use listbox.Items.Add(...) and add simple stuff like strings.  But if I add objects to the listbox and bind it to the datasource, it most assuredly doesn't work, it always changes the selection to the first item.

I'm convinced it's a bug with the control.   I haven't found any documentation about the problem.
HarmnKardnAuthor Commented:
Setting the ListBox.SelectedItem property to null accomplishes the intended goal of deselecting all items in a list bound to a DataSource.
HarmnKardnAuthor Commented:
I should note this workaround only seems to work if the .SelectionMode property is "MultiSimple".
PAQ'd and points refunded.

Community Support Moderator @Experts Exchange

Featured Post

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now