Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Drag'n'drop to a Windows form issue

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?

like image 642
BlueVoodoo Avatar asked Feb 21 '23 19:02

BlueVoodoo


1 Answers

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

like image 140
DmitryG Avatar answered Mar 05 '23 06:03

DmitryG