Page 1 of 1

How to move ListView items between ListViews

Posted: Thu Mar 01, 2012 2:08 pm
by MrAksel
Moving items from one ListView to another is not something included in the .NET Framework. This tutorial will show you how to accomplish it yourself.
First, create your Windows Forms Project and add a ListView to Form1.
We will use 4 events from the ListView: ItemDrag, DragEnter, DragOver and DragDrop.
The code for ItemDrag:
Code: Select all
    Private Sub ListView1_ItemDrag(sender As System.Object, e As System.Windows.Forms.ItemDragEventArgs) Handles ListView1.ItemDrag
        Dim items(ListView1.SelectedItems.Count - 1) As ListViewItem

        For i = 0 To ListView1.SelectedItems.Count - 1
            items(i) = ListView1.SelectedItems(i)
        Next

        Dim data As New DataObject("ListViewItem()", items)
        ListView1.DoDragDrop(data, DragDropEffects.Copy Or DragDropEffects.Move)
    End Sub
This collects every selected item in the ListView and stores them in a DataObject class.
The DataObject class is a class to store the format and data of anything that is being dragged in a drag-drop operation. Then we use the DoDragDrop method in the ListView to start the operation. The second parameters specify which effects are applied to the mouse cursor.

The next piece is the DragEnter event. It is excecuted when the mouse enters the ListView during a drag-drop operation.
Code: Select all
    Private Sub ListView1_DragEnter(sender As System.Object, e As System.Windows.Forms.DragEventArgs) Handles ListView1.DragEnter
        If e.Data.GetDataPresent("ListViewItem()") Then
            If (e.KeyState And 8) = 8 Then 'Check if control key is down
                e.Effect = DragDropEffects.Copy
            Else
                e.Effect = DragDropEffects.Move
            End If
        Else
            e.Effect = DragDropEffects.None
        End If
    End Sub

First it checks if the "ListViewItem()" format is on the drag-drop 'clipboard'. If it is, it continues to check if the Control key is down. The mouse effect is Copy if it is pressed, and only move if its not pressed. If the "ListViewItem()" data format is not available then there is no effect and the drop will not be accepted.

The 3rd handler is the DragOver event, it is executed whenever the mouse is over the ListView's bounds with something dragged. The code is the same as the DragEnter code, but it updates all the time so the the mouse cursor looks as it would either copy or move the items.

The last event to handle is the DragDrop event. When you drop the items, we have to update the items in both the ListView where the items were, and the ListView they are dropped to.
Code: Select all
    Private Sub ListView1_DragDrop(sender As System.Object, e As System.Windows.Forms.DragEventArgs) Handles ListView1.DragDrop
        If e.Data.GetDataPresent("ListViewItem()") Then
            Dim items As ListViewItem() = DirectCast(e.Data.GetData("ListViewItem()"), ListViewItem())
            If (e.KeyState And 8) = 8 Then 'Check if control key is down
                For Each i As ListViewItem In items
                    ListView1.Items.Add(i.Clone)
                Next
            Else
                For Each i As ListViewItem In items
                    i.Remove()
                Next
                ListView1.Items.AddRange(items)
            End If
        Else
            e.Effect = DragDropEffects.None
        End If
    End Sub
This checks if there is data associated with the "ListViewItem()" format. If there is, it saves the items in a ListViewItem() variabe. If the Control key is down, it clones every item and adds it to the new ListView, but if not, it removes every item from the previous ListView and adds them to the new.

Re: How to move ListView items between ListViews

Posted: Thu Mar 01, 2012 2:22 pm
by Dummy1912
great tut :)

Re: How to move ListView items between ListViews

Posted: Thu Mar 01, 2012 2:37 pm
by MrAksel
Thanks :) Ill be posting many of them now :D
I hope you come online on Skype soon!

Splendid!

Posted: Tue Mar 20, 2012 12:18 am
by anthonygz
Well done MrArskel, i really enjoyed this tutorial.