0

I have a picture icon on my form. I would like to have it so when the user drags this icon to a windows explorer, desktop etc the associated file is written to that location.

I have the file path in a combo and as I said an image on the form for the user to drag. The image is just a general image not really associated with the actual file.

Can you help me out? I have no clue!

GDutton
  • 164
  • 14
  • first you have to make an association between the picturebox (?) and the file it represents. Maybe the .Tag will work. After that it is just a File copy or move operation. – Ňɏssa Pøngjǣrdenlarp Jun 13 '14 at 12:46
  • This is really just straight ddrag-and-drop. Do you know how D-n-D works in VB.NET? If not then learning that is the first thing to do. Once you've done that, you basically just put the appropriate data on the Clipboard and Windows Explorer will handle the rest when the user does the drop. http://social.msdn.microsoft.com/Search/en-AU?query=drag%20and%20drop%20other%20applications&emptyWatermark=true&ac=5 – jmcilhinney Jun 13 '14 at 12:48
  • Well I am familiar with the idea of using Dragenter and the e.Data.Setdata to carry text to be copied out but im not sure how to do this with a file?? – GDutton Jun 13 '14 at 13:04
  • @Plutonix Is that absolutely necessary? the picture box is unable to hold the file.. ie the file is a PDF the picture box is showing a pdf icon – GDutton Jun 13 '14 at 13:07
  • If dragging the icon/image is meant to be an idiom for dragging a file, the code **must** know which file it represents. That can be done by simply storing the **filename** represented in the .Tag. After that it is a standard DragDrop resulting in a File copy or file move depending on what you intend (or perhaps which key the user holds down). – Ňɏssa Pøngjǣrdenlarp Jun 13 '14 at 13:13
  • Do you have an example of that ?? :) I either get the X you cant drag that here or it just drops text? – GDutton Jun 13 '14 at 13:21
  • gobs of code everywhere for this. you need DoDragDrop to start the process not e.Data.Setdata. [these guys](http://msdn.microsoft.com/en-us/library/system.windows.forms.control.dodragdrop(v=vs.110).aspx) have lots of examples. [see this also](http://stackoverflow.com/a/3043436/1070452) – Ňɏssa Pøngjǣrdenlarp Jun 13 '14 at 14:30

1 Answers1

0

For the next guy.... Where pbDragger is the picture box. It is important to note that the DataObject requires a string array even if you are only dragging one file.

Private Sub pbDragger_MouseMove(sender As Object, e As MouseEventArgs) Handles pbDragger.MouseMove

    If ((e.Button And MouseButtons.Left) = MouseButtons.Left) Then

        Dim strPath As String = cboRef.SelectedItem!Path.ToString ' Path of the file to be copied

        Dim strArr() As String = {strPath}  ' 'FileDrop requires an array of string!!

        Dim oDraginfo As New DataObject(DataFormats.FileDrop, strArr)
        Dim dropEffect As DragDropEffects = pbDragger.DoDragDrop(oDraginfo, DragDropEffects.Copy)

    End If
End Sub
GDutton
  • 164
  • 14