Jump to content

Help Using Listview

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
2 replies to this topic

#1
edge02

edge02

    Newbie

  • Members
  • PipPip
  • 21 posts
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.

#2
Vswe

Vswe

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 9,552 posts
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
Ray Tawil

Ray Tawil

    Programmer

  • Members
  • PipPipPipPip
  • 108 posts
Me.ListView1.SelectedItems.Item(0).Remove()

Share your Knowledge, It's one way to achieve immortality.
Video Tutorial Channel