Reputation: 1339
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.
Upvotes: 2
Views: 14394
Reputation: 22794
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);
}
Upvotes: 2
Reputation: 43300
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--;
}
}
Upvotes: 2
Reputation: 8726
Try this;
for (int i = listView1.Items.Count-1; i >=0 ; 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();
}
}
Upvotes: 0
Reputation: 2078
if you have a collection with only the selected ones why are you iterating all the elements?
just do this.
foreach(var var1 in listView1.SelectedItems.ToArray())
//the to array is to create e new collection from the list else you get one error when you change it.
{
string var2 = var1.Substring(31, 5);
... // code for other actions
listView1.Items.Remove(var1);
}
Upvotes: 0
Reputation: 40663
Use this one instead you code:
foreach(var item in listView.SelectedItems){ //todo }
Upvotes: 0
Reputation: 2785
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.
Upvotes: 1
Reputation: 9394
I think the problem is, that your index increases while listView1.Items
is getting smaller.
Upvotes: 2
Reputation: 300579
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--;
}
}
Upvotes: 2