Search code examples
c#listboxselecteditemselecteddeselect

Listbox deselect top item


I have a listbox and I want to deselect the first selected item in the list when the loop is run because it should process a list item after item. Currently I'm using this:

var list = new object[listBoxTracks.SelectedItems.Count];
for (int i = 1; i < listBoxTracks.SelectedItems.Count; i++)
    list[i - 1] = listBoxTracks.SelectedItems[i];

listBoxTracks.SelectedItems.Clear();
foreach (var track in list)
    listBoxTracks.SelectedItems.Add(track);

I think/know that this is probably very bad but I have no idea what other possibilities there are. I tried stuff with selectedIndex += 1 etc but that seems to crash it. If this has been answered before I'm sorry but I haven't found anything in my research :/


Solution

  • As far as I see, you can directly manipulate the SelectedItems. So you can also remove a single item from it, e.g.

    listBoxTracks.SelectedItems.Remove(listBoxTracks.SelectedItems[0]);