C#: Drag and Drop Tutorial

person Jason Huangfolder_openC#, Codelocal_offer, , access_time December 25, 2008

In this simple tutorial, I can show you how to easily add drag and drop to your windows form application.

Here is the steps:
– First you need to set the control’s AllowDrop property to True. (such as your main windows form of your application)


– Write your own DragDrop Event and then the DragEnter event for the control:

In the below example I implement drag n drop for my form FormFileImport, DragEventArgs e has properties for you access the file names (you can drag n drop several files). Once you get the file name, you decide what you want to do with the file names.

DragEnter event handles the cursor to display, and event when you drag on top of control before you release the button.

The code below change the cursor to the typical drag n drop icon.

See code comment for notes.

private void FormFileImport_DragDrop(object sender, DragEventArgs e)
     string[] asFiles = e.Data.GetData(DataFormats.FileDrop) as string[];
      if (asFiles != null)
                string filename = asFiles[0];
                //after you get the file name, you can decide what to do with that file name
                // such as opening the file.
private void FormFileImport_DragEnter(object sender, DragEventArgs e)
     // If the data is a file or a bitmap, display the copy cursor.
     if (e.Data.GetDataPresent(DataFormats.FileDrop))
           e.Effect = DragDropEffects.Copy;
            e.Effect = DragDropEffects.None;


  1. Very useful.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>