Results 1 to 5 of 5

Thread: Help needed on ListView

  1. #1

    Thread Starter
    New Member Caresse's Avatar
    Join Date
    Jun 2004
    Location
    Singapore
    Posts
    12

    Help needed on ListView

    Help... How do i delete a selected item from ListView?
    Below is the method i've tried using, but it doesn't work.

    VB Code:
    1. Dim i As Integer
    2.  
    3. For i = 1 To lsvNewContacts.ListItems.Count
    4.      lsvNewContacts.ListItems.Remove (i)
    5.            
    6. Next i
    For the method mentioned above, if there's only an item inside the ListView, it works well if i tried deleting that item. However, if there's 2 items in the ListView, it'll still delete the item i've selected but giving me the error 'Index out of bound' at the same time.

    Is there anyone out there that can help me with this problem? Thank you and oh by the way I am currently using visual basic 6.

  2. #2
    Fanatic Member brown monkey's Avatar
    Join Date
    Jun 2004
    Location
    Cebu
    Posts
    552
    i do this in vb.net but i don't know vb6.
    VB Code:
    1. Dim li As ListViewItem
    2.    Private Sub ListView1_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListView1.DoubleClick
    3.       ListView1.Items.Remove(li)
    4.    End Sub
    5.  
    6.    Private Sub ListView1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles ListView1.MouseDown
    7.       li = ListView1.GetItemAt(e.X, e.Y)
    8.    End Sub
    if double clicked, it removes the selected item.

  3. #3
    Hyperactive Member
    Join Date
    Mar 2004
    Location
    Prato - Tuscany - Italy
    Posts
    461
    Try this:
    Code:
      Dim lst As ListViewItem
            For Each lst In Me.LswScadenze.SelectedItems
                lst.Remove()
            Next
    Good job
    Live long and prosper (Mr. Spock)

  4. #4

    Thread Starter
    New Member Caresse's Avatar
    Join Date
    Jun 2004
    Location
    Singapore
    Posts
    12
    Both methods doesn't work but i really appreciates your help. Thanks alot. >.<

  5. #5
    Hyperactive Member
    Join Date
    Mar 2004
    Location
    Prato - Tuscany - Italy
    Posts
    461
    Uhmmm....Caresse, I did a quick test of my code. It seems work, at least for a single item (I tried in a my project where I have a ListView on which multiple selection is inibithed). It deleted the selected item in my test! My method should be able to remove multiple selected items, too.
    The code of Brown Monkey (Hi B.M., happy to meet you again! ) seems good, also. Could you describe the problem you encounter, or post some more code and details?
    Live long and prosper (Mr. Spock)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width