Reputation: 75
This error appears when I select some item in ListView for the second time. I tried to debug it, and when I select some item for the second time, the list_answers.SelectedItems.Count
is 0. Why?
Here is my code:
private void list_answers_SelectedIndexChanged(object sender, EventArgs e)
{
ListViewItem t = list_answers.SelectedItems[0];
for (int i = 0; i < tasks.Count; i++)
{
if (t.Text == "Question №" + (i + 1))
{
this.ShowOnePanel(i);
iter = i;
break;
}
}
}
Upvotes: 6
Views: 25928
Reputation: 838
If you just add this code in the listview_SelectedChangeIndex event, it will solve the problem.
if (finishListView.SelectedItems.Count > 0)
{
// here your code goes
}
else
{
return;
}
Upvotes: 3
Reputation: 15151
When changing selection, the ListView will first deselect current row and then select new one, so you will have a call where SelectedItems will be empty.
You can solve it by Adding
if(list_answers.SelectedIndex == -1)
return;
or
if(list_answers.SelectedItems.Count == 0)
return;
Upvotes: 10