Drag and Drop
I've searched the forums and internet but get no results. Is it possible to drag and drop (text, file, object, etc) onto a control and handle the events (OnDragEnter, OnDragLeave, OnDragDrop) in LinqPad?
I've searched the forums and internet but get no results. Is it possible to drag and drop (text, file, object, etc) onto a control and handle the events (OnDragEnter, OnDragLeave, OnDragDrop) in LinqPad?
Comments
I've not tried this. However, if you press Ctrl+F1, type 'event' and look at the samples on custom HTML event handling, you'll examples on how to handle any event on an HTML element. You should be able to handle the ondragstart, ondrop and ondragover events.
Thanks, Joe. I am part way there with that information. Drag & drop is somewhat working this way but I'm unable to get the data dropped. I'm also noticing that the Drop method works with html elements (even from other browser windows) but it doesn't fire for files. Some kind of security in the browser I'm guessing. Here's the code I've come up with:
LINQPad.Controls.TextArea tArea = new LINQPad.Controls.TextArea("Initial test here");
tArea.HtmlElement.AttachEventHandler("ondragenter", (sender, args) => { onDragEnter(sender, args); });
// tArea.HtmlElement.AttachEventHandler("ondragenter", (e,a) => { tArea.HtmlElement.InvokeMethod("preventDefault", null); }); // this appears to have no effect
// tArea.HtmlElement.AttachEventHandler("ondragover", (e,a) => { tArea.HtmlElement.InvokeMethod("preventDefault", null); }); // this appears to have no effect
tArea.HtmlElement.AttachEventHandler("ondrop", (sender, args) => { onDragDrop(sender, args); });
tArea.Dump();
void onDragEnter(object sender, EventArgs e)
{
Console.WriteLine("Entered!");
e.Dump("Enter EventArgs"); // yields "System.EventArgs"
//((System.Windows.Forms.DragEventArgs)e).Dump(); // InvalidCastException: Unable to cast object of type 'System.EventArgs' to type 'System.Windows.Forms.DragEventArgs'.
(e as System.Windows.Forms.DragEventArgs).Dump(); // yields null
}
void onDragDrop(object sender, EventArgs e)
{
Console.WriteLine("Dropped!");
e.Dump("Drop EventArgs");
}
Maybe you need to call preventDefault():
https://developer.mozilla.org/en-US/docs/Web/API/HTML_Drag_and_Drop_API/File_drag_and_drop