Assuming I have a Listview, when the user selects an item, I want to delete the item from the listview.
Code:
Sub dListView1_SelectedIndexChanged(Index As Int)
dListView1.Items.RemoveAt(Index)
End Sub
This code fails but the reason was not clear at the first check. Running in debug mode shows the problem.
The user selects a value, the sub is called and the item is deleted. So far so good but deleteing the item causes a SelectedIndexChanged event where the index is now -1. The code crashes.
My solution, consume the event:
Sub dListView1_SelectedIndexChanged(Index As Int)
If Index=-1 Then Return
dListView1.Items.RemoveAt(Index)
End Sub
Would seem to work but still caused problems. The better solution, store the values in a second list. Add the list to the listview as required.
Sub dListView1_SelectedIndexChanged(Index As Int)
If Index=-1 Then Return
MimeList.RemoveAt(Index)
dListView1.Items.Clear
dListView1.Items.AddAll(MimeList)
End Sub
This works without any problems.
My question: Is this behaviour normal for a listview or is this bad programming on my behalf?