I need to implement dragging and droping multiple items on a vb.net windows form between 2 listviews. I found an example but it only drags one item, and also you have to drop it onto existing items in the other listview or it doens't complete the drop. I'd like to be able to drop the item(s) onto any space in the other listview, and also what happens when you start out with no items in the other listview, you can't drop anything onto it.
Here is the relevant code:
Private Sub ListviewDrag_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'fill the listviews with some items to use
ListView1.View = View.Details
ListView1.Columns.Add("Lis
tView1 Column", 100, HorizontalAlignment.Left)
ListView1.AllowDrop = True
ListView2.View = View.Details
ListView2.Columns.Add("Lis
tView2 Column", 100, HorizontalAlignment.Left)
ListView2.AllowDrop = True
Dim x As Integer
For x = 1 To 10
ListView1.Items.Add("Listv
iew1Item" & x)
ListView2.Items.Add("Listv
iew2Item" & x)
Next
End Sub
Private Sub ListViewItemDrag(ByVal sender As Object, ByVal e As System.Windows.Forms.ItemD
ragEventAr
gs) Handles ListView1.ItemDrag, ListView2.ItemDrag
DoDragDrop(e.Item, DragDropEffects.Move)
End Sub
Private Sub ListViewDragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragE
ventArgs) Handles ListView1.DragEnter, ListView2.DragEnter
e.Effect = DragDropEffects.Move
End Sub
Private Sub ListViewDragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragE
ventArgs) Handles ListView1.DragDrop, ListView2.DragDrop
Dim lvItem As ListViewItem
Dim destItem As ListViewItem
Dim destLv As ListView = CType(sender, ListView)
Dim clX As Integer = destLv.PointToClient(New Point(e.X, e.Y)).X
Dim clY As Integer = destLv.PointToClient(New Point(e.X, e.Y)).Y
If e.Data.GetDataPresent("Sys
tem.Window
s.Forms.Li
stViewItem
", False) Then
'dragging a listview item
lvItem = CType(e.Data.GetData("Syst
em.Windows
.Forms.Lis
tViewItem"
), ListViewItem)
destItem = CType(sender, ListView).GetItemAt(clX, clY)
destLv.Items.Insert(destIt
em.Index, lvItem.Clone)
lvItem.Remove()
End If
End Sub
thanks,
bmutch
Start Free Trial