4

I have, what should be, a simple question on drag'n'drop. I have a fresh Win Form project where the form has set to allow drops using AllowDrop = true. Should also mention that I am running Windows 7 64-bit.

Just to be sure, I have subscribed to

this.DragDrop += new System.Windows.Forms.DragEventHandler(Form1_DragDrop);

as well.

But when I run the app and drag anything from my desktop or explorer, it indicates with the mouse pointer icon that I am not allowed to drop any file(s) to it after all.

I found a a similar question like this one (but Win Vista) where the issue was that Visual Studio was running with admin priveleges which windows explorer wasn't. But building the app and running the executable results in the same problem.

I have done this many times in the past and couldn't Google my way to solve this one. What am I missing?

SliverNinja - MSFT
  • 31,051
  • 11
  • 110
  • 173
BlueVoodoo
  • 3,626
  • 5
  • 29
  • 37

2 Answers2

7

By default the target drop effect of a drag-and-drop operation is not specified (DragDropEffects.None). Thus there is no drop event for your control in this case. To allow Control to be a drag-and-drop operation's receiver for the specific data you should specify the concrete DardDropEffect as shown below (use the DragEnter or DragOver events):

void Form1_DragDrop(object sender, DragEventArgs e) {
    object data = e.Data.GetData(DataFormats.FileDrop);
}
void Form1_DragEnter(object sender, DragEventArgs e) {
    if(e.Data.GetDataPresent(DataFormats.FileDrop)) {
        e.Effect = DragDropEffects.Copy;
    }
}

Related MSDN article: Performing a Drag-and-Drop Operation in Windows Forms

DmitryG
  • 17,677
  • 1
  • 30
  • 53
1

You are using the wrong Event, use the DragEnter Event.

this.DragEnter += new System.Windows.Forms.DragEventHandler(Form1_DragDrop);
Lloyd
  • 2,932
  • 2
  • 22
  • 18
  • For some reason subscribing to DragEnter works but this is not what I want. This means, I trigger the code as soon as my mouse pointer enters the form. I want it to be triggered when I release the mouse button. – BlueVoodoo Jan 19 '12 at 15:34