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!

Remove Items from a listbox

Status
Not open for further replies.
May 7, 2004
17
US
I am having trouble removing items from a listbox.

What I have is this: I have 2 listboxes and a command button. What I want to do is when an item is added to the 2nd listbox from the first, the item in the first listbox is removed.

I am still sorta new to visual basic programming and I tried everything I could think of with the .removeitem.

Thank you in advance for any and all help.
 
List1.RemoveItem List1.ListIndex

That is, if it is not a multi selectable listbox.
 
It isn't a multi-selectable list box.

It works!!! Thank you so much.
 
A more glamourous solution, useful if you want to use a listview, which is prettier!:

Copy this code I quickly knocked up into a form.
On the Form place two listviews and a command button.
Set the listviews to multiselect = true
set the view on each listview to lvwList

Then run and listview1 can move to listview2 etc.

If you have another button but swap source and target listviews round then you can move the items back and forth.

Notice i copy the contents that aren't selected to one array and the selected items to another array. If you need to keep sub items then do it on the populate part where i just copy the text at the mo, make sure you do this before clearing the source listview!!!

Martin


Option Explicit
Option Compare Binary



Private Function MoveSelectedToListView(ByVal lvwSource As ComctlLib.ListView, _
ByVal lvwTarget As ComctlLib.ListView, _
Optional ByVal RemoveSource As Boolean = True)
Dim li As ListItem
Dim i As Integer
Dim iTo As Integer
Dim iFrom As Integer
Dim arrFrom() As String
Dim arrTo() As String
Dim bItemsToMove As Boolean

For Each li In lvwSource.ListItems

If li.Selected Then
'set my flag
bItemsToMove = True
'get all selected items into one array
ReDim Preserve arrTo(iTo + 1)
arrTo(iTo) = li.Text
iTo = iTo + 1

Else
'get all unselected into another array
ReDim Preserve arrFrom(iFrom + 1)
arrFrom(iFrom) = li.Text
iFrom = iFrom + 1
End If

Next li

'now clear the source list and put the unselected items back
If bItemsToMove Then
If RemoveSource = True Then
lvwSource.ListItems.Clear
For i = 0 To UBound(arrFrom) - 1
lvwSource.ListItems.Add , , arrFrom(i)
Next i
End If
'now populate the target listbox - don't clear because items may exist already

For i = 0 To UBound(arrTo) - 1
lvwTarget.ListItems.Add , , arrTo(i)
Next i
End If

Set li = Nothing
End Function

Private Sub Command1_Click()
MoveSelectedToListView ListView1, ListView2, True
End Sub

Private Sub Form_Load()
Dim i
For i = 1 To 10
ListView1.ListItems.Add , , "item" & CStr(i)
Next i
End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top