Free Information Technology Magazines and eBooks

Monday, October 05, 2009

ListView Drag and Drop in VB.NET

While doing some code optimization on our in-house billing module that has Drag-and-Drop functionality, I decided to share the VB.NET code to beginners who want to implement Drag and Drop feature on their ListView controls. This sample VB.NET project demonstrate the technique to move items between two or more listview controls. To move multiple items back and forth between two listview, follow these steps.


1. Create your VB.NET Project. Choose Windows Application.
2. Add two ListView Control on the default form.
3. Set the AllowDrop property of each ListView control to true.
4. Set the MultiSelect property of each ListView control to true.
5. Set the View property of each ListView control to List.
6. Add Two columns on both ListView.
7. Add at least 2 sample items on ListView1.
6. Insert the following codes:


Private Sub ListView_ItemDrag(ByVal sender As System.Object, ByVal e As System.Windows.Forms.ItemDragEventArgs) Handles ListView1.ItemDrag, ListView2.ItemDrag
Dim myItems(sender.SelectedItems.Count - 1) As ListViewItem
Dim i As Integer = 0

' Loop though the SelectedItems collection for the source.
For Each myItem As ListViewItem In sender.SelectedItems
' Add the ListViewItem to the array of ListViewItems.
myItems(i) = myItem
i = i + 1
Next
' Create a DataObject containg the array of ListViewItems.
sender.DoDragDrop(New _
DataObject("System.Windows.Forms.ListViewItem()", myItems), _
DragDropEffects.Move)
End Sub
Private Sub ListView_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles ListView1.DragEnter, ListView2.DragEnter
' Check for the custom DataFormat ListViewItem array.
If e.Data.GetDataPresent("System.Windows.Forms.ListViewItem()") Then
e.Effect = DragDropEffects.Move
Else
e.Effect = DragDropEffects.None
End If
End Sub
Private Sub ListView_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles ListView1.DragDrop, ListView2.DragDrop

Dim myItems() As ListViewItem = e.Data.GetData("System.Windows.Forms.ListViewItem()")
Dim i As Integer = 0

For Each myItem As ListViewItem In myItems
' Add the item to the target list.
Dim lvItem As ListViewItem = sender.Items.Add(myItem.Text)

'Loop through the sub items and add it to the destination Listview
Dim j As Integer
For j = 1 To myItem.SubItems.Count - 1
lvItem.SubItems.Add(myItem.SubItems(j).Text)
Next

' Remove the item from the source list.
If sender Is ListView1 Then
ListView2.Items.Remove(ListView2.SelectedItems.Item(0))
Else
ListView1.Items.Remove(ListView1.SelectedItems.Item(0))
End If
i += 1
Next
End Sub

*Take note. If you changed the name of the two listview, you have to modify the above code to replace the existing listview names.

ListView Drag and Drop in VB.NET

Download the sample VB.NET Project.

For more coding tips and tricks, subscribe now.

1 comments:

Mike said...

Hi, This was helpful to me.
I have two issues I wondered if you had come up against...
1) If I release the mouse button over the source listview an exception occurs
2) If I want to use an image along with my list item it gets lost after the drag drop event

I also want to look at how I can implement a ghost image of the image use on the list item - like explorer in Vista and later

Good post - thanks