4

I have a method that removes currently selected item in a ListView

listView1.Items.Remove(listView1.SelectedItems[0]);

How do I select the next in the ListView after removing the selected one?

I tried something like

var index = listView1.SelectedItems[0].Index;
listView1.Items.Remove(listView1.SelectedItems[0]);
listView1.SelectedItems[0].Index = index;

But I get the error

Property or indexer 'System.Windows.Forms.ListViewItem.Index' cannot be 
assigned to -- it is read only

Thank you.

Abraham
  • 1,209
  • 2
  • 14
  • 22

6 Answers6

2

I had to add one more line of code to a previous answer above, plus a check to verify the count was not exceeded:

int selectedIndex = listview.SelectedIndices[0];
selectedIndex++;
// Prevents exception on the last element:      
if (selectedIndex < listview.Items.Count)
{
  listview.Items[selectedIndex].Selected = true;
  listview.Items[selectedIndex].Focused = true;
}
Crazy Cat
  • 1,332
  • 15
  • 19
1

ListView doesn't have a SelectedIndex property. It has a SelectedIndices property.

Gets the indexes of the selected items in the control.

ListView.SelectedIndexCollection indexes = this.ListView1.SelectedIndices;

foreach ( int i in indexes )
{
 //
}
Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
  • I tried adding listView1.SelectedIndices = i; in the foreach statement, but I'm getting an error. (I'm still a beginner, sorry) – Abraham Mar 21 '13 at 14:25
  • @Abraham What is the error? You can read http://msdn.microsoft.com/en-us/library/system.windows.forms.listview.aspx – Soner Gönül Mar 21 '13 at 14:26
  • Same as original "cannot be assigned to -- it is read only" and also "Cannot implicitly convert int to System.Windows.Forms.ListView.SelectedIndexCollection" – Abraham Mar 21 '13 at 14:33
  • `ListView.SelectedIndices` returns `SelectedIndexCollection` not `int` like `i`. You should use `foreach` loop to read all indexes which are selected. And then, you can use `i` as an index of these. Please read _MSDN_ page a few times.. – Soner Gönül Mar 21 '13 at 14:37
  • Okay, I will read it and come back after it working, thank you. – Abraham Mar 21 '13 at 14:43
0

try use listView1.SelectedIndices property

David
  • 15,894
  • 22
  • 55
  • 66
  • Am I missing something? All I see under "listView1.SelectedX" is SelectedIndexChanged, SelectedIndices, and SelectedItems. – Abraham Mar 21 '13 at 14:17
0

If you delete an item, the index of the "next" item is the same index as the one you just deleted. So, I would make sure you have listview1.IsSynchroniseDwithCurrentItemTrue = true and then

var index = listView1.SelectedItems[0].Index;
listView1.Items.Remove(listView1.SelectedItems[0]);
CollectionViewSource.GetDefaultView(listview).MoveCurrentTo(index);
Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
Barracoder
  • 3,696
  • 2
  • 28
  • 31
0

I've done this in the following manner:

int selectedIndex = listview.SelectedIndices[0];
selectedIndex++;
listview.Items[selectedIndex].Selected = true;
Ekster
  • 353
  • 1
  • 4
0

I actually had to do this:

        int[] indicies = new int[listViewCat.SelectedIndices.Count];
        listViewCat.SelectedIndices.CopyTo(indicies, 0);
        foreach(ListViewItem item in listViewCat.SelectedItems){
            listViewCat.Items.Remove(item);
            G.Categories.Remove(item.Text);
        }
        int k = 0;
        foreach(int i in indicies)
            listViewCat.Items[i+(k--)].Selected = true;
        listViewCat.Select();

to get it to work, none of the other solutions was working for me.

Hopefully, a more experienced programmer can give a better solution.