Skip to main content

Design a form in VB with two List boxes. Set the style property of both to 0 and 2 (Multi select) respectively. One with the left arrow and one with the right arrow. On pressing the left arrow the selected item from List Box 1 should be transferred to List Box 2 (only if it does exist in List Box 2). If the right arrow is pressed, the selected item (multiple selection are not in ListBox2) transferred to List box 1.Add items and remove items buttons for each list. Duplicate item should not be added.

· One min read
Kaustubh Kulkarni
file.vb
  
Private Sub cmdAddLeft_Click()
List1.AddItem (inputBox("Enter Item"))
End Sub

Private Sub cmdAddRight_Click()
Item = inputBox("Enter Item: ", "Left List")
For i = 0 TolstRight.ListCount - 1
If lstRight.List(i) = Item Then
MsgBox "Record Is Already Present In The List.", vbOKOnly, "Result"
Exit Sub
End If
lstRight.AddItem Item
Next
End Sub

Private Sub cmdRemoveLeft_Click()
If lstRight.ListCount = 0 Then
MsgBox ("No more items to delete")
End
Else
lstRight.RemoveItem (0)

End If
End Sub
Private Sub cmdRemoveRight_Click()
If List1.ListCount = 0 Then
MsgBox ("No more items to delete")
End
Else
List1.RemoveItem (0)
End If

End Sub

Private Sub Command1_Click()
cnt = lstRight.ListCount

If List1.ListCount = 0 Then
MsgBox ("No more items to Move")
Exit Sub
Else
For i = List1.ListCount - 1 To 0 Step -1
If List1.Selected(i) = True Then
For j = lstRight.ListCount - 1 To 0 Step -1
If lstRight.List(j) <> List1.List(i) Then
lstRight.AddItem (List1.List(i))
List1.RemoveItem (i)
Else
MsgBox ("Item "& List1.Selected(i) & "already exist")
End If

Next

End If
Next

End If

End Sub

Private Sub Command2_Click()
If List2.ListCount = 0 Then
MsgBox ("No more items to Move")
Exit Sub

Else
List1.AddItem (List2.List(ListIndex))
List2.RemoveItem (ListIndex)
End If
End Sub