Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Specified items will not be deleted when using ListView.Item.RemoveAt()

I have tried to remove specific items from a listview using the RemoveAt() method. But When I remove it the first time some items will stay.

For example: see the image below

alt text

Code:

private void button1_Click(object sender, EventArgs e)
    {
        for (int i = 0; i < listView1.Items.Count; i++) 
        {
            if (listView1.Items[i].SubItems[0].Text == "A1") 
            {
                listView1.Items.RemoveAt(i);
            }
        }
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        for(int i = 0; i<3; i++)
        {
            ListViewItem lvi = new ListViewItem("A1");
            lvi.SubItems.AddRange(new string[] {"desc" + i.ToString(), i.ToString()});
            listView1.Items.Add(lvi);
        }

        for (int i = 0; i < 2; i++)
        {
            ListViewItem lvi = new ListViewItem("A2");
            lvi.SubItems.AddRange(new string[] { "desc" + i.ToString(), i.ToString() });
            listView1.Items.Add(lvi);
        }
    }
like image 670
Rye Avatar asked Oct 29 '10 03:10

Rye


People also ask

How to remove items from ListView?

To remove items programmatically Use the RemoveAt or Clear method of the Items property. The RemoveAt method removes a single item; the Clear method removes all items from the list.


3 Answers

See MSDN in the Remarks section.

When you remove an item from the collection, the indexes change for subsequent items in the collection. All information about the removed item is deleted. You can use this method to remove a specific item from the collection by specifying the index of the item to remove from the collection. To specify the item to remove instead of the index to the item, use the Remove method. To remove all items from the collection, use the Clear method.

Edit: See Moot's answer. Link / reference above applies to his/her answer as well.

Edit 2:

Just back the counter up one if you find a match.

    for (int i = 0; i < listView1.Items.Count; i++)
    {
        if (listView1.Items[i].SubItems[0].Text == "A1")
        {
            listView1.Items.RemoveAt(i);
            i--; // Back counter up one
        }
    }
like image 84
Inisheer Avatar answered Oct 03 '22 02:10

Inisheer


When you delete the first A1, the list shrinks, and the element at 1 and 2 become your element at 0 and 1. So, when your loop increments, it deletes the A1 at 1 (the third one) and skips the one which moved down to 0.

like image 33
Kevin Stricker Avatar answered Oct 02 '22 02:10

Kevin Stricker


Do the following:

private void button1_Click(object sender, EventArgs e)
{
    var itemsToRemove = new List<ListViewItem>();

    foreach (ListViewItem item in listView1.Items)
    {
        if (item.SubItems[0].Text == "A1")
            itemsToRemove.Add(item);
    }

    foreach (var item in itemsToRemove)
        listView1.Items.Remove(item);
}

Good luck!

like image 41
Homam Avatar answered Oct 01 '22 02:10

Homam