Drag and Drop files to form or control

ImDaFrEaK

Well-known member
Joined
Jan 7, 2006
Messages
416
Location
California
Programming Experience
5-10
I am building a project in which the user can drag files from the fileviewerdialog or the windows explorer window onto my application surface. Then the listbox will simply update with the file names dropped.

I don't need all that code so please don't send it. I do need to know how to use drag and drop for files. Any code for this will be highly appreciated. Thankyou :)
 
You check for DataFormat.FileDrop and get the list of files as a string array from this clipboard object, then add each filename to listbox.
Some code I found:
VB.NET:
If Clipboard.GetDataPresent(DataFormats.FileDrop) Then
  ' Get list of files
  Dim files As String() = DirectCast(Clipboard.GetData(DataFormats.FileDrop), String())
  ' process each file in the list.
End If
 
K I have a problem, it's not working. lol. Here is the code I have and the problem I am getting.

PrivateSub ListBox1_DragDrop(ByVal sender AsObject, ByVal e As System.Windows.Forms.DragEventArgs) Handles ListBox1.DragDrop
If Clipboard.GetData(DataFormats.FileDrop) Then
' Get list of files
Dim files AsString() = DirectCast(Clipboard.GetData(DataFormats.FileDrop), String())
' process each file in the list.
Me.ListBox1.DataSource = files
EndIf
EndSub

That's all the code. I built a practice app to figure it all out. It's not doing anything. It may not be the code though, when I move a file accross my listbox or form I get a crossed out circle as a cursor; as if it's telling me I can't drop anything on to it. I have also set the allowdrop property's to True for both the Form and the Listbox.


 
Ok, I got it! But only thanks to you for getting me on the right path. I still had to do some research and I wound up copier this entire code from another site but it works perfectly. Thank you and here it is for anyone else who is curious.

VB.NET:
[SIZE=2]
[/SIZE][SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] ListBox1_DragDrop([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Object[/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Windows.Forms.DragEventArgs) [/SIZE][SIZE=2][COLOR=#0000ff]Handles[/COLOR][/SIZE][SIZE=2] ListBox1.DragDrop
[/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2] (e.Data.GetDataPresent(DataFormats.FileDrop)) [/SIZE][SIZE=2][COLOR=#0000ff]Then
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#008000]' Retrieve list of files and loop through string array
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] strFiles() [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][SIZE=2] = e.Data.GetData(DataFormats.FileDrop)
[/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].ListBox1.DataSource = strFiles
[/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]If
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Sub
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] ListBox1_DragOver([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Object[/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Windows.Forms.DragEventArgs) [/SIZE][SIZE=2][COLOR=#0000ff]Handles[/COLOR][/SIZE][SIZE=2] ListBox1.DragOver
[/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2] e.Data.GetDataPresent(DataFormats.FileDrop) [/SIZE][SIZE=2][COLOR=#0000ff]Then
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#008000]' Display the copy (or other) icon
[/COLOR][/SIZE][SIZE=2]e.Effect = DragDropEffects.Copy
[/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]If
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Sub
[/COLOR][/SIZE]

The Drag over is to tell it to show a Drag Drop Icon.
Also, make sure the Listbox or whatever your using has it's AllowDrop property set to true.
 
Back
Top