Simple drag and drop example

This example shows how to implement a basic drag and drop functionality.
This is useful if you want to drag files directly from the Windows explorer
(or any related software) into your application.

// Form load event or a similar place
private void Form_Load(object sender, EventArgs e)
{
    // Enable drag and drop for this form
    // (this can also be applied to any controls)
    this.AllowDrop = true;
 
    // Add event handlers for the drag & drop functionality
    this.DragEnter += new DragEventHandler(Form_DragEnter);
    this.DragDrop += new DragEventHandler(Form_DragDrop);
}
 
// This event occurs when the user drags over the form with 
// the mouse during a drag drop operation 
void Form_DragEnter(object sender, DragEventArgs e)
{
    // Check if the Dataformat of the data can be accepted
    // (we only accept file drops from Explorer, etc.)
    if (e.Data.GetDataPresent(DataFormats.FileDrop))
        e.Effect = DragDropEffects.Copy; // Okay
    else
        e.Effect = DragDropEffects.None; // Unknown data, ignore it
 
}
 
// Occurs when the user releases the mouse over the drop target 
void Form_DragDrop(object sender, DragEventArgs e)
{
    // Extract the data from the DataObject-Container into a string list
    string[] FileList = (string[])e.Data.GetData(DataFormats.FileDrop, false);
 
    // Do something with the data...
 
    // For example add all files into a simple label control:
    foreach (string File in FileList)
        this.label.Text += File + "\n";
}
Snippet information

Author:
Jonas John

License:
Public domain

Language:
C#

Created:
07/20/2007

Updated:
07/21/2007

Tags:
, ,

Related links:
- Drag and Drop Using C#
- Drag and Drop Image in C#.NET




Sorry folks, comments have been deactivated for now due to the large amount of spam.

Please try to post your questions or problems on a related programming board, a suitable mailing list, a programming chat-room,
or use a QA website like stackoverflow because I'm usually too busy to answer any mails related
to my code snippets. Therefore please just mail me if you found a serious bug... Thank you!


Older comments:

Kawa June 17, 2011 at 08:18
Thanks.
mitesh June 01, 2011 at 11:34
i wan to do i ahve two data grid & i want from datagrid1 & then drop in to datagrid2 the row will be copy there ......
arash November 03, 2010 at 13:37
thanks so much,i thunk i can use it in my chess game
Ronnie May 24, 2009 at 17:20
Here is a part of the source code:

string[] FileList = (string[])e.Data.GetData(DataFormats.FileDrop, false);
foreach (string File in FileList)
{
#region Video
if (File.EndsWith(".wmv")||File.EndsWith(".mpg")||File.EndsWith(".mpeg")||File.EndsWith(".avi")||File.EndsWith(".rmvb"))
{
if (v != null)
{
v.Stop();
v.Dispose();
v = null;
btPause.Visible = false;
btPlay.Visible = true;
}
v = new Video(File);
v.Owner = panel1;
v.Play();
v.Pause();
labelDuracaoOuCP.Text = (TimeSpan.FromSeconds(Convert.ToInt32(v.CurrentPosition))).ToString();
FileName = File;
tabPage1.Text = FileName;

trackPosition.Maximum = Convert.ToInt32(v.Duration);
trackVolume.Value = v.Audio.Volume;

}
e.Effect=DragDropEffects.All;
Ronnie May 24, 2009 at 17:18
Hy I'm using it with a video playback.
and when i Drag into the panel wich is the owner of the video it seems to create two objects insted of one.
how do I get only one of them?

ps: your article is great helped me alot!
Fahad March 04, 2009 at 12:32
Great Job!!!
Thanks
bora December 05, 2008 at 07:39
Thank u very much! very useful!!
haytham September 29, 2008 at 12:53
Thank u very much
:)
i test it its greate ;) and simple :)