-2

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 :/

John Saunders
  • 160,644
  • 26
  • 247
  • 397
Jalau
  • 303
  • 1
  • 2
  • 11
  • u want to uncheck the first item ? or disable it keeping the selction – Ace McCloud Apr 19 '15 at 20:31
  • I want to unselect/uncheck the first selected item. So if I call listBoxTracks.SelectedItems[0] the next item is called instead. So everytime I call the code the next item is processed. – Jalau Apr 19 '15 at 20:33
  • Did you know that .NET has at least 4 different ListBox classes? It helps when you indicate what your GUI platform is. – H H Apr 19 '15 at 21:32

2 Answers2

3

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]);
azt
  • 2,100
  • 16
  • 25
  • Do I just unselect it with this or completly remove it with this? Thanks for your answer :) – Jalau Apr 20 '15 at 04:14
  • Since you remove it from the SelectedItems collection, only it's selected state is removed. You can remove it completely from the list by removing it from the Items collection. – azt Apr 20 '15 at 06:36
  • By the way, there was another answer that is now deleted. The user suggested that the SelectedItem property always holds the first selected item (or null if none is selected). So you can use it instead of taking the 0th element. In any case, make sure to check for the case that nothing is selected. – azt Apr 20 '15 at 06:40
0
  private void checkedListBox1_SelectedValueChanged(object sender, EventArgs e)
        {
            checkedListBox1.SetItemChecked(checkedListBox1.SelectedIndex, true);
        }

        private void checkedListBox1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            checkedListBox1.SetItemChecked(checkedListBox1.SelectedIndex, true);
        }

This Will Do It for Sure , wanted this for me as well...

Kuza Grave
  • 1,256
  • 14
  • 15