Hi, can anyone tell me the codes on how to remove an item from a listview with multiple items?
i know the event is listview_SelectedIndexChanged but i dont know the codes to remove the selected item. Can anyone please help me.
Heres what i have done so far:
Private Sub ListView2_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListView2.SelectedIndexChanged
Dim i As Integer
For i = 0 To ListView2.Items.Count - 1
If ListView2.Items(i).Selected = True Then
If MsgBox("Would you like to remove the item?", MsgBoxStyle.Information, "Information") = MsgBoxResult.Ok Then
ListView2.Items(i).Remove()
End If
End If
Next
End Sub
but i'm having errors when i delete an item especially if its in the middle of the list.
Help Using Listview
Started by edge02, Apr 03 2010 06:34 AM
2 replies to this topic
#1
Posted 03 April 2010 - 06:34 AM
|
|
|
#2
Posted 03 April 2010 - 06:55 AM
After you've deleted one item the numbers of item won't be the same as before and therefor the variable i will be greater then the number of items, therefore you should exit the loop after removing the item:
Private Sub ListView2_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListView2.SelectedIndexChanged
Dim i As Integer
For i = 0 To ListView2.Items.Count - 1
If ListView2.Items(i).Selected = True Then
If MsgBox("Would you like to remove the item?", MsgBoxStyle.Information, "Information") = MsgBoxResult.Ok Then
ListView2.Items(i).Remove()
[B][COLOR="red"]Exit For[/COLOR][/B]
End If
End If
Next
End Sub
#3
Posted 03 April 2010 - 11:58 PM
Me.ListView1.SelectedItems.Item(0).Remove()
Share your Knowledge, It's one way to achieve immortality.
Video Tutorial Channel
Video Tutorial Channel


Sign In
Create Account


Back to top









