Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Drag and Drop not working in C# Winforms Application

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?

like image 207
bornfromanegg Avatar asked Oct 29 '14 10:10

bornfromanegg


2 Answers

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.

like image 156
Tea With Cookies Avatar answered Sep 21 '22 17:09

Tea With Cookies


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

like image 30
Valery Letroye Avatar answered Sep 23 '22 17:09

Valery Letroye