Very often you are asked to do this very basic and essential task in VB.NET applications. It is to move Items from one Listbox to another Listbox. Today we are going to solve this problem. First of all you need to create the design screen. Here it is with all the controls and names I have used in this assignment.

Move Items from one listbox to another listbox

I have used two listboxes-lst1 and lst2. The list box property “SelectionMode” of both listboxes is set to “MultiExtended”. I have used four command buttons  with following names

  • btnLTRall (to move all items from lst1 to lst2)
  • btnLTRsel(to move selected items from lst1 to lst2)
  • btnRTLall (to move all items from lst2 to lst1)
  • btnRTLsel  (to move selected items from lst2 to lst1)

In this application a user defined procedure is created that takes two list boxes as arguments. This procedure is called to move Items from one Listbox to another Listbox. The procedure uses an array that stores all the selected items from the source listbox. From this array the count of items is assigned to variable num. num variable is used to define the number of times the do while loops are to be executed to move elements from one listbox to another listbox . It is also used to remove the moved items from the source listbox. The “add” method of items collection of listbox is used to add the items in the destination listbox. The “remove” method of items collection of listbox is used to remove items from the source listbox.

When the application is executed the first listbox is filled with the system color names.  This code is used to fetch the color names into a string array colNames . colNames  is then used to fill the color names in the first listbox i.e. lst1.

Dim colNames As String() = [Enum].GetNames(GetType(KnownColor))

Here is the complete code for the application

 
 
Public Class MoveListItems
    Private Sub Form3_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim i As Byte
        'Add the colors to the listbox 1
        Dim colNames As String() = [Enum].GetNames(GetType(KnownColor))
        Do While i < colNames.Length - 1
            lst1.Items.Add(colNames(i))
            i = i + 1
        Loop
        lst1.Sorted = True
    End Sub
    Private Sub btnLTRall_Click(sender As Object, e As EventArgs) Handles btnLTRall.Click
        Dim i As Integer
        Do While i <= lst1.Items.Count - 1
            lst2.Items.Add(lst1.Items(i))
            i = i + 1
        Loop
        lst1.Items.Clear()
    End Sub
    Private Sub btnLTRsel_Click(sender As Object, e As EventArgs) Handles btnLTRsel.Click
        movedata(lst1, lst2)
        lst2.Sorted = True
    End Sub
    Private Sub btnRTLsel_Click(sender As Object, e As EventArgs) Handles btnRTLsel.Click
        movedata(lst2, lst1)
        lst1.Sorted = True
    End Sub
    Private Sub btnRTLall_Click(sender As Object, e As EventArgs) Handles btnRTLall.Click
        Dim i As Integer
        Do While i <= lst2.Items.Count - 1
            lst1.Items.Add(lst2.Items(i))
            i = i + 1
        Loop
        lst2.Items.Clear()
    End Sub
    Private Sub movedata(l1 As ListBox, l2 As ListBox)
        Dim i, num As Integer
        Dim strv(20) As String
        i = 0
        'get the total number of selected items
        num = l1.SelectedItems.Count
        'move the selected items in an array
        Do While i <= num - 1
            strv(i) = l1.SelectedItems(i).ToString
            i = i + 1
        Loop
        i = 0
        'move the selected items from array to second listbox
        Do While i <= num - 1
            l2.Items.Add(strv(i))
            i = i + 1
        Loop
        i = 0
        'remove the selected items from array to first listbox
        Do While i <= num - 1
            l1.Items.Remove(strv(i))
            i = i + 1
        Loop
    End Sub
End Class