how to move a listbox item if you have 5 listboxes

theazza

Member
Joined
May 20, 2011
Messages
23
Programming Experience
1-3
im having trouble with moving listbox items between several listboxes with the click of a button
 
What exactly are you having trouble with?


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



Hope that helps
 
thanks for trying but that did work. to move an item between 2 listboxes is easy:


Dim entry1 As String = lstWedmo.SelectedItem

'lstWedmo.Items.Remove(entry1)
'lstWedcl.Items.Add(entry1)

however to move the same item between 5 lisboxes i cant do
 
Back
Top