|
|
Title | Drag and drop one or more multi-column values from one ListView control to another in Visual Basic .NET |
Description | This example shows how to drag and drop one or more multi-column values from one ListView control to another in Visual Basic .NET. |
Keywords | drag, drop, drag and drop, objects, ListView, Visual Basic .NET, VB .NET |
Categories | Controls |
|
|
When you press the right mouse button over a ListView, the MouseDown event handler starts the drag. It creates a List(Of ListViewItem) and copies references to the currently selected items into it.
The code then calls the control's DoDragDrop method to start the drag, allowing the copy and move actions.
The call to DoDragDrop returns when the drag is finished. The method returns a value indicating whether the drag resulted in a move, copy, or nothing (in this case). If the drag ended with a move, then the code removes the selected items from the ListView that started the drag.
|
|
Private Sub ListView_ItemDrag(ByVal sender As Object, ByVal _
e As System.Windows.Forms.ItemDragEventArgs) Handles _
lvwAssigned.ItemDrag, lvwUnassigned.ItemDrag
Dim lvw As ListView = DirectCast(sender, ListView)
Dim dragged_items As New List(Of ListViewItem)
For Each selected_item As ListViewItem In _
lvw.SelectedItems
dragged_items.Add(selected_item)
Next selected_item
Dim result As DragDropEffects = _
lvw.DoDragDrop(New _
DataObject("System.Collections.Generic.List(Of " & _
"System.Windows.Forms.ListViewItem)", _
dragged_items), DragDropEffects.Move)
' Remove the dragged items if necessary.
If result = DragDropEffects.Move Then
For Each selected_item As ListViewItem In _
dragged_items
lvw.Items.Remove(selected_item)
Next selected_item
End If
End Sub
|
|
If the drag moves over a ListView, the DragOver event gives the control a chance to allow or prohibit the drag. The following code checks whether the drag can provide a List(Of ListViewItem) as data. If so, then it checks whether the Ctrl key is down and allows a move or copy effect.
|
|
Private Sub ListView_DragEnter(ByVal sender As Object, _
ByVal e As System.Windows.Forms.DragEventArgs) Handles _
lvwAssigned.DragEnter, lvwUnassigned.DragEnter
' Check for the custom DataFormat ListViewItem array.
If e.Data.GetDataPresent("System.Collections.Generic.List(Of " & _
"System.Windows.Forms.ListViewItem)") Then
e.Effect = DragDropEffects.Move
Else
e.Effect = DragDropEffects.None
End If
End Sub
|
|
Finally, when the user drops, the DragDrop event handler deals with the dropped data. If the drop can provide a List(Of ListViewItem) and this is a copy or move, the code gets the data.
The event handler's e parameter gives the location where the drop occurred in screen coordinates. The code uses the control's PointToClient method to convert the coordinates into the control's coordinate system. It then uses the GetItemAt method to learn the index of the item where the drop occurred.
The code then loops through the list of items, adding them to the target ListView. One catch is that a ListViewItem cannot be in more than one ListView at the same time. To avoid problems adding the ListViewItem to the drop target, the code clones each ListViewItem and adds them to the ListView.
|
|
Private Sub ListView_DragDrop(ByVal sender As Object, ByVal _
e As System.Windows.Forms.DragEventArgs) Handles _
lvwAssigned.DragDrop, lvwUnassigned.DragDrop
Dim lvw As ListView = DirectCast(sender, ListView)
Dim dragged_items As List(Of ListViewItem) = _
e.Data.GetData("System.Collections.Generic.List(Of " & _
"System.Windows.Forms.ListViewItem)")
Dim pt As Point = lvw.PointToClient(New Point(e.X, e.Y))
Dim lvi As ListViewItem = lvw.GetItemAt(pt.X, pt.Y)
Dim index As Integer
If lvi IsNot Nothing Then
index = lvi.Index
End If
For Each list_view_item As ListViewItem In dragged_items
' Add the item to the target list.
Dim new_item As ListViewItem = _
DirectCast(list_view_item.Clone(), ListViewItem)
If lvi Is Nothing Then
lvw.Items.Add(new_item)
Else
lvw.Items.Insert(index, new_item)
index += 1
End If
Next list_view_item
End Sub
|
|
|
|
|
|