Sometimes you might have two list boxes side by side, and want to double-click on an item in one box to move it to the other.
For instance, maybe the first list box contains a list of customers, and you want to perform some function (say, email them) that pertains to only a few of those customers. By double-clicking on the customer names you want, you get the desired list in the right hand box.
First, make a generic sub that takes two listbox arguments:
-
-
Private Sub MoveListItem(lstFrom As ListBox, lstTo As ListBox)
-
lstTo.AddItem (lstFrom.Value)
-
lstFrom.RemoveItem (lstFrom.Value)
-
End Sub
-
Then call this sub in the double-click events for the respective list boxes:
-
-
Private Sub lstA_DblClick(Cancel As Integer)
-
MoveListItem Me.lstA, Me.lstB
-
End Sub
-
-
Private Sub lstB_DblClick(Cancel As Integer)
-
MoveListItem Me.lstB, Me.lstA
-
End Sub
-
Here you'll replace "lstA" and "lstB" with whatever the names of your list boxes are. You could modify this slightly by having a button situated between the boxes, and move the item from one to the other when the button is clicked.
It's a simple task but can come in handy!
Pat