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?
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With