Remove items from ListView in C #

I need to remove items from the ListView , the code I'm looking for will show a MessageBox for confirmation, and if the item is not selected, it will show a MessageBox error.

This is my code and it does not work :(

 private void button2_Click(object sender, EventArgs e) { if (listView1.SelectedItems != null) { var confirmation = MessageBox.Show( "Voulez vous vraiment supprimer les stagiaires séléctionnés?", "Suppression", MessageBoxButtons.YesNo, MessageBoxIcon.Question ); if (confirmation == DialogResult.Yes) { for (int i = 0; i < listView1.Items.Count; i++) { if (listView1.Items[i].Selected) { listView1.Items[i].Remove(); i--; } } } } else { MessageBox.Show("aucin stagiaire selectionnes", "erreur", MessageBoxButtons.OK, MessageBoxIcon.Error); } } 

The error is not to delete, but in MessageBox's I have two MessageBox's , the error must be shown first before confirmation.

+4
source share
6 answers

Start counting from end to zero

 for (int i = listView1.Items.Count - 1; i >= 0; i--) { if (listView1.Items[i].Selected) { listView1.Items[i].Remove(); } } 

However, keep in mind that each ListViewItem has an Index property, and using this collection has the advantage of avoiding redundant test and cyclic switching to fewer elements.

(Note: the collection of SelectedItems is never null, if there is no selection, the collection is empty but not null)

So your code can be rewritten

 if (listView1.SelectedItems.Count > 0) { var confirmation = MessageBox.Show("Voulez vous vraiment supprimer les stagiaires séléctionnés?", "Suppression", MessageBoxButtons.YesNo, MessageBoxIcon.Question); if (confirmation == DialogResult.Yes) { for (int i = listView1.SelectedItems.Count - 1; i >= 0; i--) { ListViewItem itm = listView1.SelectedItems[i]; listView1.Items[itm.Index].Remove(); } } } else MessageBox.Show("aucin stagiaire selectionnes", ...); 
+9
source

You should not reference the original collection that you use during the iteration, but some others:

 foreach(ListViewItem item in listView1.Items) if (item.Selected) listView1.Items.Remove(item); 
+2
source

You can use only this code without decrement

 listView1.Items[i].Remove(); 

Note. You can also use the RemoteAt method by specifying the position

0
source

You need to change the MessageBox confirmation from Show to ShowDialog . This will make it modal and waiting for the result.

You need to check emptry for "SelectedItems"

0
source

You can change the code as follows. Note that the ListView.SelectedIndices collection contains the indices of the selected ListViewItems . Just repeat them from the end to the beginning, and you will not need to process index updates, but leave them in the for loop:

  if (listView1.SelectedIndices.Count>0) { var confirmation = MessageBox.Show("Voulez vous vraiment supprimer les stagiaires séléctionnés?", "Suppression", MessageBoxButtons.YesNo, MessageBoxIcon.Question); if (confirmation == DialogResult.Yes) { for (int i = listView1.SelectedIndices.Count-1; i >= 0; i--) { listView1.Items.RemoveAt(listView1.SelectedIndices[i]); } } } else MessageBox.Show("aucin stagiaire selectionnes", "erreur", MessageBoxButtons.OK, MessageBoxIcon.Error); 
0
source
 //if (lvPhotos.SelectedIndices.Count > 0) if (lvPhotos.CheckedIndices.Count > 0) { var confirmation = MessageBox.Show("Supprimer les photos séléctionnées ?", "Confirmation", MessageBoxButtons.YesNo, MessageBoxIcon.Question); if (confirmation == DialogResult.Yes) { // selected //for (int i = lvPhotos.SelectedIndices.Count - 1; i >= 0; i--) //{ // lvPhotos.Items.RemoveAt(lvPhotos.SelectedIndices[i]); //} // checked for (int i = lvPhotos.CheckedIndices.Count - 1; i >= 0; i--) { lvPhotos.Items.RemoveAt(lvPhotos.CheckedIndices[i]); } } } 
0
source

All Articles