10

I'm puzzled with this. I attempted implementing drag and drop on a DataGridView. Failing to see any events fired, I tried a simple form, with a text box.

I would like to be able to drag files or folders from Windows Explorer.

I'm missing something because these events never fire. I did read about DragEvents, Windows 7 and UIPI but I still couldn't get around this.

I'm out of ideas and I welcome your suggestions.

public Form1()
{
    InitializeComponent();
    this.AllowDrop = true;
    textBox1.AllowDrop = true;
    textBox1.DragEnter += new DragEventHandler(textBox1_DragEnter);
    textBox1.DragDrop += new DragEventHandler(textBox1_DragDrop);
    textBox1.DragOver += new DragEventHandler(textBox1_DragOver);
}

void textBox1_DragOver(object sender, DragEventArgs e)
{
    e.Effect = DragDropEffects.Move;
}

void textBox1_DragDrop(object sender, DragEventArgs e)
{
    e.Effect = DragDropEffects.Move;
}

void textBox1_DragEnter(object sender, DragEventArgs e)
{
    e.Effect = DragDropEffects.Move;
}

It seems that this should work. I have a clean install on WP7 64 - with all updates, I don't have virus or malware protection running, or anything (to my knowledge) which could prevent these events firing.

Community
  • 1
  • 1
nullable
  • 2,513
  • 1
  • 29
  • 32
  • 1
    You're trying to drag from your application to another application ? – Yochai Timmer Jan 08 '12 at 10:38
  • The form's DragEnter event handler won't fire, you didn't subscribe the event. No obvious reason why the text box' event won't fire. Set a breakpoint on it and drag a file from Windows Explorer to the box. – Hans Passant Jan 08 '12 at 11:44
  • @YochaiTimmer I updated the post, I am trying to drag files from Explorer. – nullable Jan 08 '12 at 21:36
  • @HansPassant I wired this up in the designer. I've removed it from the example. None of these events fire - that's my issue. – nullable Jan 08 '12 at 21:37
  • Well, you current snippet just doesn't repro the problem nor is there an obvious explanation for the failure. Try it on another machine. Btw, very strange to have a constructor named "form1". The boilerplate one is always Form1, capital eff. *Always* copy-paste a snippet, don't type it in by hand. – Hans Passant Jan 08 '12 at 21:53
  • @HansPassant The boiler plate was Form1 - good catch. I have pasted the code. Just to confirm, when you use this code it fires DragEvent? I will try another machine. – nullable Jan 08 '12 at 22:16
  • I"m facing this same issue. but with a dataGridView and tabControl. Everything works fine as long as my drag-n-drop source is from another app. How do I get this to work with another winForm instance created from with my current project? – Max Eisenhardt May 06 '13 at 19:26

2 Answers2

45

I had the same issue. it was only because I was debugging from a "run as administrator" session. I think that since VISTA there is a security that prevents from dropping to a privileged application.

fumble
  • 466
  • 6
  • 4
  • 6
    This is the kind of stuff that breaks your head – Alexander Derck Jan 12 '16 at 13:23
  • 5
    You deserve a medal for this solution. – Mohnkuchenzentrale Jan 23 '17 at 14:43
  • 1
    sorry to put a "thank you!!!!" comment... this problem is an ackward behavior that windows not notifies anything about that, damn 3 hours trying to make a drag&drop work... – FabianSilva Sep 18 '17 at 19:49
  • 1
    I could not agree more with the statement *This is the kind of stuff that breaks your head*; I spent hours figuring out why the events weren't firing ... I was debugging from a *Run as Administrator* session. (I am using Windows 10) – AAsk Jul 19 '19 at 22:08
  • Thank you for saving me three hours of design time! – MSD Nov 06 '19 at 21:03
5

I found that while I was running my Forms application in debug mode from Visual Studio, it didn't work. Only when I ran it outside of VS does it work perfectly. Presumably this is also something to do with security on Windows 7 (and possibly later versions).

andyb
  • 91
  • 1
  • 2