Solved

VB.Net 4.5 Drag and Drop to Explorer

Posted on 2015-01-27
2
545 Views
Last Modified: 2015-01-27
I've looked high and low for a solution to being able to Drag and Drop from a Picturebox to Explorer to copying the file in the folder location.   Take a look at my code below.  I need another set of eyes on it to see what I'm getting wrong here.  Thanks.

Basically, I'm saving the image to a temporary location upon the mouse down event, putting it into the data object, they trying to drop the object upon drop.  Everything seems to work except I get no file.   :(  


    Private Sub Picturebox1_QueryContinueDrag(ByVal sender As Object, ByVal e As System.Windows.Forms.QueryContinueDragEventArgs) Handles PictureBox1.QueryContinueDrag
        'ESC pressed
        If e.EscapePressed Then
            e.Action = DragAction.Cancel
            Return
        End If
        'Drop!
        If e.KeyState = 0 Then
            e.Action = DragAction.Drop
            Kill(strTempFile)
            Return
        End If
        e.Action = DragAction.Continue
    End Sub

    Private Sub Picturebox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseDown

        m_MouseIsDown = True

    End Sub

    Private Sub Picturebox1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseMove

        If m_MouseIsDown Then

            Dim dataObj As DataObject
            dataObj = New DataObject

            strTempFile = Path.GetTempPath & "drawing-" & trItem.IRECID & "-" & TimeOfDay.Hour & TimeOfDay.Minute & TimeOfDay.Second & ".jpg"
            PictureBox1.Image.Save(strTempFile)

            dataObj.SetData(DataFormats.FileDrop, strTempFile)
            DoDragDrop(dataObj, DragDropEffects.Copy)

        End If

        m_MouseIsDown = False

    End Sub
0
Comment
Question by:trelectric
2 Comments
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 500 total points
ID: 40573803
Try this out...

Note that I'm doing a MOVE, which means we don't have to Kill() the temp file unless the operation is cancelled; since the tmp file will actually move to the new location.  Also note the you have to pass a STRING ARRAY to the DataObject even though it is only one item:  
    Private Sub Picturebox1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseMove
        If e.Button = Windows.Forms.MouseButtons.Left Then
            strTempFile = Path.Combine(Path.GetTempPath, "drawing-" & trItem.IRECID & "-" & TimeOfDay.Hour & TimeOfDay.Minute & TimeOfDay.Second & ".jpg")
            PictureBox1.Image.Save(strTempFile)

            Dim dataObj As New DataObject(DataFormats.FileDrop, New String() {strTempFile})
            PictureBox1.DoDragDrop(dataObj, DragDropEffects.Move)
        End If
    End Sub

    Private Sub Picturebox1_QueryContinueDrag(ByVal sender As Object, ByVal e As System.Windows.Forms.QueryContinueDragEventArgs) Handles PictureBox1.QueryContinueDrag
        If e.Action = DragAction.Cancel Then
            Kill(strTempFile)
        End If
    End Sub

Open in new window

0
 

Author Closing Comment

by:trelectric
ID: 40573972
IT WORKS!  I can stop banging my head against the desk now.  I figured it had to be a string array since I seen other examples as such.  My bad.
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question