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!

RemoveItem Troubles

Status
Not open for further replies.

OzzieBloke

Programmer
Mar 28, 2004
31
AU
Hi, I am having trouble with RemoveItem.
This is what I have so far:
Code:
For N = 0 To List1.ListCount - 1
    If List1.Selected(N) = True Then
        List1.RemoveItem List1.ListIndex
    End If
Next

I have also tried:
Code:
For N = 0 To List1.ListCount - 1
    If List1.Selected(N) = True Then
        List1(Index).RemoveItem List1(Index).ListIndex
    End If
Next

Every time I run the program, it removes the first 2 items from the list and then comes up with an error.
 
Hmm...

Try adding STEP 1 to your FOR NEXT Loop for me please...

mmilan
 
Did you mean
Code:
For N = 0 To List1.ListCount - 1
    If List1.Selected(N) = True Then
        List1(Index).RemoveItem List1(Index).ListIndex
    End If
    N = N + 1
Next
??

This works for the first selected item in the list, but not any of the others, or on the second time the button is clicked. However the error does not come up anymore.
 
Reverse the direction of the loop.
___
[tt]
For N = [tt]List1.ListCount - 1 To 0 Step -1[/tt]
If List1.Selected(N) Then
List1.RemoveItem List1.ListIndex
End If
Next[/tt]
 
OK the error is completely gone, however, no items are being removed from the list.
 
You also need to correct the body of the loop.
___
[tt]
For N = List1.ListCount - 1 To 0 Step -1
If List1.Selected(N) Then
List1.RemoveItem [tt]N[/tt]
End If
Next[/tt]
 
OK this is my whole code for that button
Code:
Private Sub Command1_Click()
'Copy selected files to the other ListBox
For N = 0 To List1.ListCount - 1
    If List1.Selected(N) = True Then
        List2.AddItem List1.List(N)
    End If
Next

'Remove selected files from original ListBox
For N = 0 To List1.ListCount - 1 Step -1
    If List1.Selected(N) Then
        List1.RemoveItem N
    End If
Next
End Sub
But it still isn't performing the second half of the code.
 
You did not change the direction of the second loop properly. See the code in blue.
___
[tt]
Private Sub Command1_Click()
'Copy selected files to the other ListBox
For N = 0 To List1.ListCount - 1
If List1.Selected(N) = True Then
List2.AddItem List1.List(N)
End If
Next

'Remove selected files from original ListBox
For N = [tt]List1.ListCount - 1 To 0[/tt] Step -1
If List1.Selected(N) Then
List1.RemoveItem N
End If
Next
End Sub[/tt]
___

It seems that you are trying to make a 'Mover Listbox' in which selections are transferred from one listbox to another. If this is so than see thread222-585425.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top