Invalid argument value of '1' is not valid for 'index'

11,690

Solution 1

Selected items contains only those selected, and yet you are iterating over the entire collection.

  for (int i = 0; i < listView1.Items.Count; i++)
    {
    if (listView1.Items[i].Selected)
      {
        string var1 = listView1.Items[i].ToString();  // <-------
        string var2 = var1.Substring(31, 5); 
        ... // code for other actions
        listView1.Items[i].Remove();
        i--;
      }
    }

Solution 2

I think the problem is, that your index increases while listView1.Items is getting smaller.

Solution 3

You check for Items to start with but then check the index against SelectedItems

If there are 4 elements in Items and only the 4th is selected then SelectedItems has 1 item but i would be 4

  for (int i = 0; i < listView1.SelectedItems.Count; i++)
    {
        string var1 = listView1.SelectedItems[i].ToString();
        string var2 = var1.Substring(31, 5); 
        ... // code for other actions
        listView1.Items[i].Remove();
        i--;
      }
    }

Solution 4

var1 needs to be from Items, not from SelectedItems. Like this:

private void delete_button_Click(object sender, EventArgs e)
{
  for (int i = 0; i < listView1.Items.Count; i++)
    {
    if (listView1.Items[i].Selected)
      {
        string var1 = listView1.Items[i].ToString(); //NOTE THE DIFFERENCE
        string var2 = var1.Substring(31, 5); 
        ... // code for other actions
        listView1.Items[i].Remove();
        i--;
      }
    }

In fact though, a better way to do this would be like this:

private void delete_button_Click(object sender, EventArgs e)
{
  foreach (var x in listView1.SelectedItems.Select(x => x))
    listView1.Items.Remove(x);
}

Solution 5

This is because you are iterating through the items in the list box, and not the selected items. For example if you have 10 items in the box, and you have selected 2, when it gets to the thrird iteration it will fail.

Share:
11,690
D-Lef
Author by

D-Lef

I rock.

Updated on June 05, 2022

Comments

  • D-Lef
    D-Lef almost 2 years

    I have a listview and I want when a button is pressed to delete the selected item. Additionally, I use the item for some other actions. Basically, I use some letters of the item' s string to match with a file and delete it. This works if the selected item is the first one in the listview, but doesn' t work if it's second, third etc.

    private void delete_button_Click(object sender, EventArgs e)
    {
      for (int i = 0; i < listView1.Items.Count; i++)
        {
        if (listView1.Items[i].Selected)
          {
            string var1 = listView1.SelectedItems[i].ToString(); //error
            string var2 = var1.Substring(31, 5); 
            ... // code for other actions
            listView1.Items[i].Remove();
            i--;
          }
        }
     }
    

    It thorws error

    ArgumentOutofRangeException was not handled" - Invalid argument value of '1' is not valid for 'index'

    I don't understand what's the problem and why it works only if it' s the first item.