Drag'n'drop for windows form problem

I have what it should be, a simple drag'n'drop question. I have a new Win Form project in which the form is resolved using AllowDrop = true . It should also be mentioned that I am running the 64-bit version of Windows 7.

To be sure, I signed up for

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

.

But when I launch the application and drag something from my desktop or explorer, it points with the mouse pointer icon, which I am not allowed at all in it.

I found a similar question similar to this one (but Win Vista), in which the problem was that Visual Studio worked with admin privileges that were not in Windows Explorer. But creating an application and running executable results in the same problem.

I have done this many times in the past and Google could not find a way to solve this problem. What am I missing?

+4
source share
2 answers

By default, the target effect of dropping the drag operation is not specified (DragDropEffects.None). So in this case there is no drop event for your control. To allow the Office to be the receiver of drag and drop operations for specific data, you must specify a specific DardDropEffect, as shown below (use 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

+7
source

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

 this.DragEnter += new System.Windows.Forms.DragEventHandler(Form1_DragDrop); 
+1
source

Source: https://habr.com/ru/post/1391867/


All Articles