- Home /
Question by
chezwaldo · Jul 01, 2016 at 07:19 AM ·
drag and drop
Reason why IDataObject.GetData from System.Windows.Forms returning empty string[] on a drag and drop event
I'm trying to make it so users can add their own textures to a program I'm making and I'm doing it but creating a Windows Form that'll receive a drag and drop event and send that data back in. Here's a stripped down version of the code.
using System.Windows.Forms; public class DragAndDrop : MonoBehaviour { Form DropForm; void Start() { }
public void ShowDropDialog()
{
print("starting window");
DropForm = new DragDropForm();
DropForm.ShowDialog();
}
class DragDropForm : Form
{
public DragDropForm()
{
this.AllowDrop = true;
this.Width = 300;
this.Height = 300;
this.Text = "Drag Images here";
this.DragDrop += new DragEventHandler(this.DragDropForm_DragDrop);
this.DragEnter += new DragEventHandler(this.DragDropForm_DragEnter);
}
void DragDropForm_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop))
{
e.Effect = DragDropEffects.Link;
this.BackColor = System.Drawing.Color.Blue;
}
string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);
foreach (string file in files)
{
}
}
void DragDropForm_DragDrop(object sender, DragEventArgs e)
{
print("dropped");
object data = e.Data.GetData(DataFormats.FileDrop);
this.BackColor = System.Drawing.Color.Green;
string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);
foreach (string file in files)
{
}
print("closing");
this.Close();
}
}
}
The problems are the Drop event doesn't fire at all, hovering with a dragged file gives the crossed out circle like it doesn't let me drop it, and the e.Data.GetData only returns an empty string instead of a filepath. It really frustrating as I have no idea if I'm doing something wrong.
Comment
Your answer