Invalid argument value of '1' is not valid for 'index'
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.
No comments:
Post a Comment