if you want to move items between listboxes i think you can just use the remove, removeAt, add, or insert functions of the items.
lstBox1.items.add(obj)
lstBox1.items.removeAt(indexNumber)
'lstBox1.items.'...and so forth...
I don't think there is a function that swaps items between listboxes? You can write a simple swap function if thats what your trying to do?
one way to move an item is to just remove the item from one listbox using removeAt() or remove() and then using add() or insert() to add it to the targeted list box, below are some quick example functions i wrote.
Private Sub swapClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
swap(ListBox1.Items(ListBox1.SelectedIndex), ListBox2.Items(ListBox2.SelectedIndex))
'swapSelectedItem(ListBox1, ListBox2)
'replaceSelectedItem(ListBox1, ListBox2)
End Sub
Private Sub moveClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
moveSelectedTo(ListBox1, ListBox2)
'moveSelectedTo(ListBox1, ListBox2,true)
End Sub
Sub swap(ByRef a As Object, ByRef b As Object)
Dim buffer As Object = a
a = b
b = buffer
End Sub
'could just use the swap() written above in this function instead of this round about way...
Sub swapSelectedItem(ByRef lstA As ListBox, ByRef lstB As ListBox)
Dim selectedIndexA As Integer = lstA.SelectedIndex
Dim selectedIndexB As Integer = lstB.SelectedIndex
Dim bufferFromA As Object = lstA.Items(selectedIndexA)
lstA.Items(selectedIndexA) = lstB.Items(selectedIndexB)
lstB.Items(selectedIndexB) = bufferFromA
End Sub
Sub replaceSelectedItem(ByRef lstA As ListBox, ByRef lstB As ListBox)
Dim selectedIndexA As Integer = lstA.SelectedIndex
Dim selectedIndexB As Integer = lstB.SelectedIndex
Dim itemA As Object = lstA.Items(selectedIndexA)
Dim itemB As Object = lstB.Items(selectedIndexB)
If (itemA <> Nothing) And (itemB <> Nothing) Then
lstA.Items.RemoveAt(selectedIndexA)
lstA.Items.Insert(selectedIndexA, itemB)
lstB.Items.RemoveAt(selectedIndexB)
lstB.Items.Insert(selectedIndexB, itemA)
End If
End Sub
Sub moveSelectedTo(ByRef lstBoxWithSelected As ListBox, ByRef lstBoxToMoveTo As ListBox, Optional ByVal moveToSelection As Boolean = False)
Dim selectedIndex As Integer = lstBoxWithSelected.SelectedIndex
If moveToSelection Then
Dim moveToSelectedIndex As Integer = lstBoxToMoveTo.SelectedIndex
lstBoxToMoveTo.Items.Insert(moveToSelectedIndex, lstBoxWithSelected.Items(selectedIndex))
Else
lstBoxToMoveTo.Items.Add(lstBoxWithSelected.Items(selectedIndex))
End If
lstBoxWithSelected.Items.RemoveAt(selectedIndex)
End Sub
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.