22

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?

bornfromanegg
  • 2,826
  • 5
  • 24
  • 40
  • Did you check if you accidentially lost the connection between DragDrop event and Form1_DragDrop handler? Sorry, if you did. – Fratyx Oct 29 '14 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. – DonBoitnott Oct 29 '14 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 07 '18 at 20:17
  • @Archlight -- does anyone know why such ridiculousness exists? – rory.ap Oct 22 '18 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 '18 at 06:27
  • My upvote for @Archlight didn't work, so noting here: Admin vs Lower priority user was all my problem was. Run both as Admin and all works fine. – BRebey Feb 05 '19 at 18:22

9 Answers9

39

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.

Tea With Cookies
  • 950
  • 6
  • 18
  • 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 '16 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 '22 at 07: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

Valery Letroye
  • 1,035
  • 11
  • 19
  • 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? – bornfromanegg Jun 03 '18 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... – Valery Letroye Jun 04 '18 at 17:20
  • 1
    I cannot thank you enough. I wasted so much time on this. – agarcian Nov 16 '18 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 07 '21 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.

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

gottsche
  • 83
  • 4
0

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

Ammar
  • 152
  • 2
  • 12
  • 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. – Anthony Mason Dec 08 '16 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.

Ray
  • 7,940
  • 7
  • 58
  • 90
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.

Jeff
  • 1
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.

Night94
  • 965
  • 8
  • 11