Into more useful territory yet again. The beloved Listview and Treeview controls are the most common controls you will see using D&D in applications. I've chosen the Listview because I managed to find an example of a TreeView on Microsoft's site and I decided to adapt it for the Listview which differs in only one way but which might cause problems. For reference the Treeview example can be found here.
So lets go straight to the code and I'll explain:-
Private Sub ListViewItemDrag(ByVal sender As Object, ByVal
e As System.Windows.Forms.ItemDragEventArgs) 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.DragEventArgs)
Handles ListView1.DragEnter, ListView2.DragEnter
e.Effect = DragDropEffects.Move
End Sub
Private Sub ListViewDragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs)
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("System.Windows.Forms.ListViewItem",
False) Then
'dragging a listview item
lvItem = CType(e.Data.GetData("System.Windows.Forms.ListViewItem"),
ListViewItem)
destItem = CType(sender, ListView).GetItemAt(clX,
clY)
destLv.Items.Insert(destItem.Index,
lvItem.Clone)
lvItem.Remove()
End If
End Sub
Comments