Solved

VB.Net 4.5 Drag and Drop to Explorer

Posted on 2015-01-27
2
401 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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

707 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now