How to move ListView items between ListViews

Heres your chance to share your own tutorials with the community. Just post them on here. If your lucky they may even be posted on the main site.
4 posts Page 1 of 1
Contributors
User avatar
MrAksel
C# Coder
C# Coder
Posts: 1758
Joined: Fri Mar 26, 2010 12:27 pm

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.
You do not have the required permissions to view the files attached to this post.
LMAOSHMSFOAIDMT
Laughing my a** of so hard my sombrero fell off and I dropped my taco lmao;


Over 30 projects with source code!
Please give reputation to helpful members!

Image
Image
User avatar
Dummy1912
VIP - Donator
VIP - Donator
Posts: 1969
Joined: Sat Aug 21, 2010 2:17 pm

great tut :)
visit us on:


http://www.softpedia.com/get/System/Lau ... -Run.shtml
Check it out ! http://www.softpedia.com/publisher/I-A- ... 90017.html
Check it out ! http://www.softpedia.com/get/Desktop-En ... lock.shtml
User avatar
MrAksel
C# Coder
C# Coder
Posts: 1758
Joined: Fri Mar 26, 2010 12:27 pm

Thanks :) Ill be posting many of them now :D
I hope you come online on Skype soon!
LMAOSHMSFOAIDMT
Laughing my a** of so hard my sombrero fell off and I dropped my taco lmao;


Over 30 projects with source code!
Please give reputation to helpful members!

Image
Image
User avatar
anthonygz
Member
Member
Posts: 48
Joined: Sun Feb 13, 2011 7:25 pm

Splendid!
anthonygz
Well done MrArskel, i really enjoyed this tutorial.
4 posts Page 1 of 1
Return to “Tutorials”