Drag N Drop with a listview

TyB

Well-known member
Joined
May 14, 2009
Messages
102
Programming Experience
3-5
Hello,
I have a listview that displays images in the large icon view. I placed a picturebox ontop of the listview in the bottom corner so that the user could drag items onto the picture box.

I stupidly thought it would be easy to do drag n drop where when you drag an image it would follow the mouse. WRONG.

I CANNOT get any type of drag n drop to work at all.

I would appreciate any help.

Thanks,
Ty
 
I would even be happy with.....

Changing the mouse cursor when the mouse is held down in the listview to simulate drag n drop.

Ty
 
Still not there

John the information you provided helped to get the correct events wired however still having some issues.

I created a nice cursor from an icon and I tested the cursor in the application I made and it worked great. I added it to my project and also embedded it (which never shows up in the list when using (My.Resources.).

So I tried changing the the listview1 cursor like this.
Cursor.Current = New System.Windows.Forms.Cursor(Application.StartupPath() & "\DragNDrop.cur")

and I get an image format is invalid. Now I read that the System.Windows.Forms.Cursor namespace does not accept colored images.

Surely there is a way to change the mouse pointer to a color cursor. I mean the program I used to create the cursor was able to display it as a cursor.

If anyone has some insight on how to change the mouse cursor to a custom color cursor I would appreciate it.

Ty
 
Finally got it working...

I was finally able to get this to work changing the icon of mouse when dragging. Not sure why I could not get John's method to work. What this does is place an image at the tip of the mouse arrow simulating the dragging of the object. I accomplished this by placing a panel on the form and setting it's visible property to false. I then placed a global variable in one of my module to hold the image that will be placed into the panel and a variable to use to check if a drag operation is happening.

VB.NET:
    Public myBitmap As System.Drawing.Bitmap
    Public booldrag As Boolean

Then in the form load event I place this code.

VB.NET:
'Set up image that will be used a s the drag icon in the listview.
        myBitmap = New System.Drawing.Bitmap(Application.StartupPath() & "\Copy-32.png")

Then in the Listview1.ItemDrag event I place this code to say a drag operation is happening.

VB.NET:
Private Sub ListView1_ItemDrag(ByVal sender As Object, ByVal e As System.Windows.Forms.ItemDragEventArgs) Handles ListView1.ItemDrag

        booldrag = True
    End Sub

I then place this code into the Listview1.MouseMove event to place the image at the location of the pointer and move it with the mouse pointer.

VB.NET:
Private Sub ListView1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles ListView1.MouseMove
        If booldrag = True Then
            Panel1.Visible = True

            Dim pt As Point = PointToClient(MousePosition)

            Panel1.Location = pt

        End If
    End Sub

And finally we add this code to place the image onto the panel.

VB.NET:
    Private Sub Panel1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Panel1.Paint
        e.Graphics.DrawImage(myBitmap, 1, 1, 32, 32)
    End Sub

Not the most elegant way to do this, but it works for my purposes and hopefully will help someone else.

Thanks,

Ty
 
Back
Top