23

I am trying to create a windows form onto which I can drop a file/folder.

I have the following code in a WinForms app

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_DragEnter(object sender, DragEventArgs e)
    {
        Debug.Print("DragEnter");
    }

    private void Form1_DragDrop(object sender, DragEventArgs e)
    {
        MessageBox.Show("Dropped!");
    }
}

I have set the AllowDrop property to true. I've tried running the application in debug within Visual Studio. Based on answers to other similar questions, I've tried running the compiled exe as administrator. I've tried running the compiled exe not as administrator.

But whatever I do, I cannot get the DragDrop event to fire. The DragEnter event does fire, however. What am I missing?

6
  • Did you check if you accidentially lost the connection between DragDrop event and Form1_DragDrop handler? Sorry, if you did.
    – Fratyx
    Oct 29, 2014 at 10:59
  • Is it a plain form, or have you added controls? If you drop onto a form's control, it will be that control that is responsible. Oct 29, 2014 at 11:00
  • 4
    Drag and Drop will not work if you are running Visual Studio / your application as administrator and Windows Explorer is running under your normal account.
    – Archlight
    Feb 7, 2018 at 20:17
  • @Archlight -- does anyone know why such ridiculousness exists?
    – rory.ap
    Oct 22, 2018 at 13:37
  • Security. It used to be COM communication wrapped so it looked like Win32 api and it would be a BAD idea to allow one user running on the box to access another users memory.... How it is today, probably the same.
    – Archlight
    Oct 23, 2018 at 6:27

9 Answers 9

40

Is your DragDropEffect set appropriately? Try placing this in the DragEnter Event Handler Method:

    private void Form1_DragEnter(object sender, DragEventArgs e)
    {
        Console.WriteLine("DragEnter!");
        e.Effect = DragDropEffects.Copy;
    }

By default it was set to DragDropEffects.None so the Drop event wouldn't fire.

2
  • 4
    Oh my god THANK YOU SIR/MADAM! I have been looking for this fo so long, and I couldn't find it anywhere.
    – Voidpaw
    Aug 27, 2016 at 12:33
  • Sorry for the very late reply but is there any information on WHY this is needed and what it does? I've seen this answer in many forums but no one explains why it's needed.
    – dsanchez
    Jul 27, 2022 at 7:16
29

For those who would read this because tips above do not work.

Notice that Drag&Drop won't work if you run Visual Studio or your app "As Administrator" as reported here: https://visualstudio.uservoice.com/forums/121579-visual-studio-ide/suggestions/2164233-fix-drag-and-drop-to-open-file-when-running-as-adm

4
  • That looks like it’s referring to drag and drop within Visual Studio itself, not applications running in Visual Studio. Or am I missing something? Jun 3, 2018 at 10:28
  • Oups, indeed. But it was nevertheless my issue. I as unable to drag and drop files on my WinForm application started in debug mode within VS.Net, if this one was run as admin... Jun 4, 2018 at 17:20
  • 1
    I cannot thank you enough. I wasted so much time on this.
    – agarcian
    Nov 16, 2018 at 18:56
  • 2
    This answer is underrated. Debugging from VS with admin privilege won't work but if you run the program from /bin/debug folder via file explorer you'd notice dragdrop works.
    – Giddy Naya
    May 7, 2021 at 17:02
6

Don't forget in properties of the form to change AllowDrop to "True" Your code is probably fine but if this property is not enabled to true it won't work. It is set to false by default.

2
  • AllowDrop was set to True. The accepted answer is the correct one. Jul 9, 2017 at 12:27
  • Does it matters when i change AllowDrop in runtime Jun 6, 2020 at 12:36
4

try to use something like this in your Form1_DragEnter:

private void Form1_DragEnter(object sender, DragEventArgs e)
{
    if (e.Data.GetDataPresent(DataFormats.FileDrop))
        e.Effect = DragDropEffects.All;
    else
    {
        String[] strGetFormats = e.Data.GetFormats();
        e.Effect = DragDropEffects.None;
    }
}

this fires your Form1_DragDrop

0
0

Have you written the MouseDown and MouseMove events of the object you are dragging from.

1
  • Not quite sure why this was downvoted. I use this approach and it works very well. if (e.LeftButton == MouseButtonState.Pressed) { DragDrop.DoDragDrop(this.dragItem, this.dragItem, DragDropEffects.Copy); } And on mouse move, I simply set e.Handled to true use a boolean called is dragging to detect if the user is still moving the item when the event fires. Each area for dropping has its very own event for handling what was dropped where. Dec 8, 2016 at 20:05
0

Another very nasty and tricky problem can be that you have overriden OnHandleCreated, but forgot to call the base implementation. Then your application fails to set the required internal window settings to respect your AllowDrop property.

E.g., make sure to call base.OnHandleCreated(e) in your override, and you'll be fine.

0

I also had this perplexing issue despite the form having AllowDrop set to true!

In my Windows Forms app (VS2017) I had to make sure I had set a valid Startup object: e.g myprojectname.Program and all was fine!

0

I had a command line specified that was pointing to a file that no longer existed. Somehow that was preventing drag enter from firing. Once I removed it everything was fine again.

0

Probably a rare scenario, but maybe helpful to someone one day:

None of the above worked for me. My break point in Form_DragDrop was never hit.

It turned out that code inside the handler function was calling a static function of a different DLL that failed to load (BadImageException). This error was silently ignored and the entire event handler was just not called.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.