Move Items Between List Boxes


Posted by JAF on July 27, 2000 4:26 AM

I have a List Box (imaginatively called ListBox1), which is populated with specific data when one of 7 option buttons is selected.

What I want to be able to do is to have a CommandButton which moves from ListBox1 (which contains ALL valid entries relating to whichever option button is selected) to ListBox2 those entries highlighted by the user.

For instance: ListBox1 may contain "Pool 1", "Pool 2", "Pool 3", "Pool 4" & "Pool 5", but if the users only selects "Pool 1" and "Pool 4", I want just those entries added to ListBox2.

I was given some code to do this (ListBox2.AddItem ListBox1.Value), but this generates the following error message:
Run Time Error '-2147352571 (80020005)':
Type Mismatch

Any suggestions?



Posted by JAF on July 27, 0100 5:07 AM

Update on Problem

I forgot to mention that the ListBoxes are both set up as MultiSelect, but I have since managed to solve the problem, only to encounter 2 more!

The code I am using to move the items across is as follows:
Private Sub cmd_Add_Click()
Dim i As Integer
If ListBox1.ListIndex = -1 Then Exit Sub
For i = ListBox1.ListCount - 1 To 0 Step -1
If ListBox1.Selected(i) = True Then
ListBox2.AddItem ListBox1.List(i)
ListBox1.RemoveItem i
End If
Next i
End Sub

This works fine, however, when I run the Remove_Click code (in which ListBox1 is changed to ListBox2 and vice versa), the item moved back is placed at the BOTTOM of ListBox1 - Is there any way to replace the item in the position in which it started?

The Other problem I've just noticed is that when the OptionButton is clicked to populate the ListBox, if another OptionButton is clicked immediately afterwards, the valid items for that option are ADDED to the bottom of the existing values that were populated when the first option button was clicked.

What's the best way to clear the previous ListBox data when the user clicks multiple ObjectButtons in succession?

Hope this makes sense.


JAF