Results 1 to 4 of 4

Thread: Trouble Removing Items from a list

  1. #1
    tbone231
    Guest

    Trouble Removing Items from a list

    Hi Again,

    I am able to move my items from one list to another using this code....
    Private Sub cmdaddone_Click()
    Cart = ""

    For i = 0 To lstcart.ListCount - 1
    If lstcart.Selected(i) = True Then
    Cart = Cart & lstcart.List(i)
    End If
    Next i

    lstcart.List(i) = lststore.List(lststore.ListIndex)
    End Sub

    However, When I try to remove items from the new list I have created I seem to be running into errors. I just want to delete the item from the lstcart all together. I'm not sure why it won't work.

    Here is that code.

    Private Sub cmdremoveone_Click()


    For i = 0 To lstcart.ListCount - 1
    If lstcart.Selected(i) = True Then
    Cart = Cart & lstcart.RemoveItem(i)
    End If
    Next i

    lstcart.List(i) = lststore.List(lststore.ListIndex)


    End Sub

    suggestions?

  2. #2
    Fanatic Member
    Join Date
    Aug 2000
    Posts
    736
    This line "Cart = Cart & lstcart.RemoveItem(i)" is most likely causing the problem. You are setting a variable = to itself & the value of the RemoveItem method. I do not think you can do that.

    You might try "Cart = Cart & lstCart.SelectedItem(i)" (or whatever the proper syntax is). And then place lstCart.RemoveItem(i) on a separate line so it can execute.

  3. #3
    tbone231
    Guest

    Thanks

    Once again, some simple help did the trick for me

    Thanks a lot !!!

  4. #4
    PowerPoster BruceG's Avatar
    Join Date
    May 2000
    Location
    New Jersey (USA)
    Posts
    2,657
    The crux of your problem is that when you remove multiple items from a listbox, you must do so in reverse - otherwise the listindex gets messed up. Here's an example with a listbox called "lstFood":

    Code:
    Dim intListX    As Integer
    
    For intListX = (lstFood.ListCount – 1) To 0 Step -1
        If lstFood.Selected(intListX) Then
              lstFood.RemoveItem intListX
        End If
    Next
    To move items from one listbox to another, you can use the following code ("lstAvail" is the list of all available items, "lstSelected" is the is list of only the ones you want from the first list):
    Code:
        Dim intListX            As Integer
        
        For intListX = lstAvail.ListCount – 1 To 0 Step -1
            If lstAvail.Selected(intListX) Then
                lstSelected.AddItem lstAvail.List(intListX)
                ' If you are using the optional ItemData property array, 
                ' add the following line to carry the "ItemData baggage" along ...
                 lstSelected.ItemData(lstSelected.NewIndex) = lstAvail.ItemData(intListX)
                lstAvail.RemoveItem intListX
            End If
        Next
    You could make a general purpose Sub to do this:
    Code:
    Public Sub MoveListBoxItems(lstListToAddTo As ListBox, _
                                lstListToRemoveFrom As ListBox)
       
        Dim intListX            As Integer
        
        For intListX = lstListToRemoveFrom.ListCount – 1 To 0 Step -1
            If lstListToRemoveFrom.Selected(intListX) Then
                lstListToAddTo.AddItem lstListToRemoveFrom.List(intListX)
                lstListToAddTo.ItemData(lstListToAddTo.NewIndex) _
                             = lstListToRemoveFrom.ItemData(intListX)
                lstListToRemoveFrom.RemoveItem intListX
            End If
        Next
    
    End Sub
    Assuming you had two command buttons called "cmdAdd" and "cmdRemove", with the above Sub incorporated into your project, you could use the following code in the cmdAdd_Click and cmdRemove_Click events to call the MoveListBoxItems Sub:

    Code:
    Private Sub cmdAdd_Click()
    		
        MoveListBoxItems lstSelected, lstAvail
    
    End Sub
    
    
    Private Sub cmdRemove_Click()
    		
        MoveListBoxItems lstAvail, lstSelected
    
    End Sub
    "It's cold gin time again ..."

    Check out my website here.

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