Drag and Drop Files

Audax321

Well-known member
Joined
May 4, 2002
Messages
90
I wanted to initialize a drag from my program as a file... Kind of like Windows Explorer does. I found a way to do this on VB6 (see code below). But Im not sure how to add this to my program in VB.NET. Currently I have it dragging a string containing the path to the file.

Here is the VB6 code:
Code:
Private Sub Picture1_OLEStartDrag(Data As DataObject, AllowedEffects As Long)
   this evant allows us to implement dragging *from* our picturebox to other drag targets in the system
  
   make sure there is a file name in the tag property (you would probaby want to use a "file
   exists" routine here but for this demo we are doing it simply)
  If Picture1.Tag <> "" Then
    On Error GoTo EH
    
     set the drop effect to create a copy of the file wherever it gets dropped
    AllowedEffects = vbDropEffectCopy
    
    With Data
       clear the contents of the data object
      .Clear
       tell the data object we will be dragging files
      .SetData , vbCFFiles
       add the file name that we storred in the tag eairler
      .Files.Add Picture1.Tag
    End With
  End If
  
ExitNow:
  Exit Sub
  
EH:
  Err.Clear
  MsgBox "Unable to initiate drag."
  Resume ExitNow
  
End Sub
 
I think something like this is what you need:

Code:
    Private Sub PictureBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseDown
        Dim s(0) As String
        Dim d As DataObject

        s(0) = "c:\test.txt"
        d = New DataObject(DataFormats.FileDrop, s)

        PictureBox1.DoDragDrop(d, DragDropEffects.Copy Or DragDropEffects.Move)
    End Sub
 
That is what I want, but it doesnt work. I made a file search utility that searches specified directories for files... I want to be able to drag (copy) these files onto the desktop (or any other folder) from their current location.

I also want any mp3s found to be dragged into WinAmp. Dragging a string containing the path doesnt work. I think WinAmp requires a path to be dragged as a file.
 
Sorry, that code works perfectly. Replacing the path with the full path to an MP3 worked, dragging it in to winamp. When you drag files, all you are really dragging is a string array of paths anyway.

If youre using a listview as your source for dragging multiple files, it has an ItemDrag event to cater for multiple items being dragged, so you use that instead of MouseDown.
 
Woohoo, just checked my code, I forgot to replace the path in the listbox.dodragdrop command with the variable for the data object .... it worked...

Thanks... :)
 
Back
Top