Deselect in ListBox reselects first item...

Gladimir

Regular
Joined
Mar 29, 2003
Messages
60
Location
San Diego, CA
I think I am doing something wrong with my listbox control. The Selection Mode is multi-extended and the DataSource is bound to a dataset table resulting from a query.

Everything works wonderfully, until I deselect the last selected item in the Listbox. When I deselect the last selected item in the Listbox, by holding the Control key and clicking the last selected item, the first item in the list becomes selected.

I have code in only one event for this listbox and that is the SelectedIndexChanged event and here is the code:

C#:
private void listMatches_SelectedIndexChanged
   (object sender, System.EventArgs e)
{
   buttonExclude.Enabled = false;
   buttonCommit.Enabled = false;

   // update exclude/commit buttons
   if (listMatches.SelectedItems.Count > 0)
   {
      buttonExclude.Enabled = true;
      buttonCommit.Enabled = true;
   }

   labelSelected.Text = listMatches.SelectedItems.Count.ToString();
}
 
Last edited:
Yeah I'm with Robby here, the above code doesn't seem to do anything that would suggest selecting another item in the list. Perhaps you should run it in debug mode and step through each line of code to find out what exact line is causing the strange selection to occur.
 
Might be a bug...

Well, I've done some experimentation and think this could be a bug in the ListBox control.

I started a new project, threw a listBox on the form, set the SelectMode to MultiExtended, and populated it use the Add method.

The multi-select and deselect worked as expected.

However, when I populate the same ListBox by assigning a DataSet table to the DataSource property, I get the disfunctional multi-select and deselect behavior.

Any ideas?
 
Try binding the Table's Default View and see if that solves the problem.

It may very well be an odd bug with the ListBox.
 
Back
Top