How do I create drag and drop between 2 list boxes?

There is also an attached project


Private Sub lblLabel1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lblLabel1.Click
        If ListBox1.Items.Count = 0 Then Exit Sub ' Avoid no items error

        ' 1. Copy it
        ListBox2.Items.Add(ListBox1.SelectedItem)
        ' 2. Delete original
        ListBox1.Items.Remove(ListBox1.Items(ListBox1.SelectedIndex))

        If ListBox1.Items.Count > 0 Then ListBox1.SelectedIndex = 0 ' Avoid Null value error

    End Sub 

drag listbox.zip (20.3 KB)

Here is another way to do this…
First, you have to make sure the AllowDrop properties of each list box is set to TRUE.

Here’s the code that I used and it works fine:


Private Sub lbxEmployee_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles lbxEmployee.MouseDown
        If lbxEmployee.SelectedIndex < 0 Then Return
        lbxEmployee.DoDragDrop(lbxEmployee.Items(lbxEmployee.SelectedIndex).ToString, DragDropEffects.Copy Or DragDropEffects.Move)
End Sub

Private Sub lbxCrew_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles lbxCrew.DragEnter
        e.Effect = DragDropEffects.All
End Sub

Private Sub lbxCrew_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles lbxCrew.DragDrop
        lbxCrew.Items.Add(e.Data.GetData(DataFormats.Text).ToString)
        lbxEmployee.Items.Remove(lbxEmployee.Items(lbxEmployee.SelectedIndex))
End Sub

Private Sub lbxCrew_DoubleClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lbxCrew.DoubleClick
        lbxEmployee.Items.Add(lbxCrew.Items(lbxCrew.SelectedIndex))
        lbxCrew.Items.Remove(lbxCrew.Items(lbxCrew.SelectedIndex))
End Sub

The first list box is named lbxEmployee and contains a list of Employee names. The 2nd list box is named lbxCrew. With the above code, the user can drag and drop names from the first list onto the 2nd list, and remove names from the 2nd list by double-clicking on them and have the name put back onto the 1st list.
It works quite fine, but I’ll probably add arrow buttons to also move names back and forth.

Thanks all for the input and help.

Edit: Note, I’ve found a problem with this code and I need some help to fix it. The problem is that if the user presses the left mouse button (MouseDown event) and then releases it while still in the same list, the DragDrop event is still triggered, so what happens is the selected item is duplicated in the list. If anyone can think of a good way to prevent this, then please post it. Thanks.

Edit2: I guess a good way to prevent this would be to check to make sure which list box the user is in before allowing the Items.Add part. Does anyone know how to check whick listbox the user is clicking in? Then you could institue an IF statement before doing the Items.Add.

Edit3: Ok, I’ve answered my own question again. I inserted this IF statement and it seems to work:


If lbxCrew.FindStringExact(e.Data.GetData(DataFormats.Text).ToString) = -1 Then

Only problem now is that the doubleclicking part doesn’t seem to want to work, so I just removed that and am just using the drag and drop method.

Here is the complete code:


Private Sub lbxEmployee_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles lbxEmployee.MouseDown
        If lbxEmployee.SelectedIndex < 0 Then Return
        lbxEmployee.DoDragDrop(lbxEmployee.Items(lbxEmployee.SelectedIndex).ToString, DragDropEffects.Copy Or DragDropEffects.Move)
End Sub

Private Sub lbxCrew_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles lbxCrew.MouseDown
        If lbxCrew.SelectedIndex < 0 Then Return
        lbxCrew.DoDragDrop(lbxCrew.Items(lbxCrew.SelectedIndex).ToString, DragDropEffects.Copy Or DragDropEffects.Move)
End Sub

Private Sub lbxCrew_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles lbxCrew.DragEnter
        e.Effect = DragDropEffects.All
End Sub

Private Sub lbxEmployee_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles lbxEmployee.DragEnter
        e.Effect = DragDropEffects.All
End Sub

Private Sub lbxCrew_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles lbxCrew.DragDrop
        If lbxCrew.FindStringExact(e.Data.GetData(DataFormats.Text).ToString) = -1 Then
            lbxCrew.Items.Add(e.Data.GetData(DataFormats.Text).ToString)
            lbxEmployee.Items.Remove(lbxEmployee.Items(lbxEmployee.SelectedIndex))
        End If
End Sub

Private Sub lbxEmployee_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles lbxEmployee.DragDrop
        If lbxEmployee.FindStringExact(e.Data.GetData(DataFormats.Text).ToString) = -1 Then
            lbxEmployee.Items.Add(e.Data.GetData(DataFormats.Text).ToString)
            lbxCrew.Items.Remove(lbxCrew.Items(lbxCrew.SelectedIndex))
        End If
End Sub