mabezat
mabezat

Reputation: 103

Delete Items from ListView in C#

I need to delete items from a ListView, the code I am looking for will show a MessageBox to confirm and if no item is selected it will show an error MessageBox

This is my code and it is not working :(

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 in delete but, in MessageBox's I have two MessageBox's, error must be shown first before confirmation.

Upvotes: 9

Views: 69805

Answers (6)

Steve
Steve

Reputation: 216363

Start counting from the end going to zero

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

However consider that every ListViewItem has an Index property and you can use it with the SelectedItems collection in such a way to prepare a solution that has the advantages to avoid a redundant test and a loop on lesser number of items.

Also, the SelectedItems collection is never null, if no selection is present, then the collection is empty but not null.

So your code could 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", ...);

Upvotes: 17

Sinatr
Sinatr

Reputation: 22008

You should not reference the original collection you are using during iteration, but some other:

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

Upvotes: 2

user1800216
user1800216

Reputation:

//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]);
                    }
                }
            }

Upvotes: 1

Thelonias
Thelonias

Reputation: 2935

You need to change your confirmation MessageBox from Show to ShowDialog. This will make it modal and wait for a result.

You need to check for emptry on "SelectedItems"

Upvotes: 0

Nikola Davidovic
Nikola Davidovic

Reputation: 8666

You can change the code like this. Note that ListView.SelectedIndices collection holds the indexes of the selected ListViewItems. Just iterate them from the end towards beginning and you won't need to deal with index updates but leave them to 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);

Upvotes: 0

Aghilas Yakoub
Aghilas Yakoub

Reputation: 29000

You can use just this code without -- decrement

listView1.Items[i].Remove();

Note : You can also use RemoteAt method by specifing position

Upvotes: 0

Related Questions