Search Forum
(53671 Postings)
Search Site/Articles

Archived Articles
712 Articles

C# Books
C# Consultants
What Is C#?
Download Compiler
Code Archive
Archived Articles
Advertise
Contribute
C# Jobs
Beginners Tutorial
C# Contractors
C# Consulting
Links
C# Manual
Contact Us
Legal

GoDiagram for .NET from Northwoods Software www.nwoods.com


 
Printable Version

Drag and Drop Files with C#
By Dave Ceddia

Download DragDropFiles.zip

This tutorial will explain how to handle drag and drop events sent to one of the windows in your program by the Windows shell. In other words, this tutorial will teach you how to allow users to drag and drop files onto your form so you can do stuff with them. This was bugging me for a little while, because it was so simple in VB and for some reason it took me a while to figure it out in C#...anyway, on to the tutorial! This is the first tutorial I've ever written, so bear with me :)

To start, create a new C# Windows Application project called DragDropFiles. Put a ListBox control somewhere on the form. Set its AllowDrop property to "true". Click the Events lightning bolt to get to the events of the ListBox.

Now, to be able to handle files being dropped, at least two events need to be dealt with. The first is DragEnter, where basically all you have to do is allow the drag operation to continue. If you don't allow the operation to continue, say, because the user is dragging text into the control, the mouse cursor will stay a "NO" symbol instead of becoming the "Copy Here" symbol (the pointer with a "+" next to it).

The second event that must be handled (assuming they get past the DragEnter event) is DragDrop. This is where you actually read the files in the list and, in this application, add them to a ListBox.

So, scroll down in the events list (make sure you have the ListBox selected, not your form) until you find DragEnter and doubleclick it. Enter the following code in the listBox1_DragEnter method:

// make sure they're actually dropping files (not text or anything else)
if( e.Data.GetDataPresent(DataFormats.FileDrop, false) == true )
// allow them to continue
// (without this, the cursor stays a "NO" symbol
e.Effect = DragDropEffects.All;
The comments should explain the code. Next, we need to actually handle the drop. Get back to the graphical form editing interface and select the ListBox. Make sure you are viewing the Events for the ListBox and scroll down to the DragDrop event and doubleclick it. Enter the following code into the listBox1_DragDrop method:
// transfer the filenames to a string array
// (yes, everything to the left of the "=" can be put in the 
// foreach loop in place of "files", but this is easier to understand.)
string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);

// loop through the string array, adding each filename to the ListBox
foreach( string file in files )
{
listBox1.Items.Add(file);
}
You're done! That was simple :) Go run the program and drag and drop some files (My Computer and the Recycle Bin don't count as files) onto the ListBox and see what happens. It will add the filename with FULL PATH to the ListBox, so if you want just the filename you'll need to parse it out (something I won't talk about here). The example code shows this in action with everything done for you, if you're that kind of person :) The example code also demonstrates how to grab the size of the file and put it into a label -- something I was too lazy to explain here. Have fun, and happy coding!