Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem with Removing items in Listbox

Status
Not open for further replies.

logius

Programmer
Aug 30, 2001
175
US
Alright, here's the situation, I've got a userform that has two listboxes (SendBox and ReceiveBox). What I'm trying to do is list the selected items from SendBox in ReciveBox (which I've already done). After that, I want to remove the selected items from SendBox. Later on I'm going to need the data from RecieveBox for something else, but I don't want repeated data (so I don't want the user to be able to select the data again).

What I tried doing was setting up an array that holds the index numbers of the selected items. After I dump the data into RecieveBox, I try and remove the entries from SendBox using the index array. The problem is that it's not working. Not all the items that were selected end up being deleted.

Here's the code:
Code:
Private Sub cmdAddListItem_Click()
Dim arrRemove() As Integer     'array storing index numbers
Dim e As Integer    'generic use variable

    e = Me.SendBox.ListCount
    ReDim arrRemove(1 To e)
    
    e = 1
    With Me.SendBox
        For i = 0 To .ListCount - 1
            If .Selected(i) Then
                Me.ReceiveBox.AddItem .List(i)
                arrRemove(e) = i
                e = e + 1
            End If
        Next i
        
        For i = 1 To e - 1
          .RemoveItem arrRemove(i)  
        Next i
        
    End With
    
End Sub
As you can tell, this code is bound to a command button.

Sample data would be: (all data would be located in a single row beginning at location E3)
Jun-96
Jul-96
Aug-96
Sep-96
Oct-96
Nov-96
Dec-96
Jan-97
Feb-97
Mar-97
Apr-97
May-97
Jun-97
Jul-97

Any help would be appreciated.
 
Okay, I figured out why the items aren't being deleted... the indexes of the items in the SendBox are being altered everytime one of them is deleted. Does anyone know of a way to get around this, besides searching manually for each item?
 
Nevermind, I figued it out *grumble*. Just replaced the old for loop:
Code:
For i = 1 To e - 1
     .RemoveItem arrRemove(i)
     For g = 1 To e - 1
          arrRemove(g) = arrRemove(g) - 1
     Next g
Next i
 
Hi!

Maybe this will work: ***Note, this is untested code***

Dim cntl as Control
Dim varItem as Variant

Set cntl = Me.SendBox

For Each varItem in cntl.ItemsSelected
Me.ReceiveBox.AddItem cntl.List(varItem)
cntl.RemoveItem varItem
Next varItem

Set cntl = Nothing

That way you remove them as you go.

It might be more intuitional for the user if you would put this type of code in the double click routine since most would be used to moving an item using a double click.

hth
Jeff Bridgham
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top