snorepion snorepion - 1 year ago 110
C# Question

Why isn't DragDrop enabled textbox accepting files?

private void textBox1_DragDrop(object sender, DragEventArgs e)
{
FileInfo fi = new FileInfo((string)e.Data.GetData(DataFormats.FileDrop));
byte[] ba = Encoding.Default.GetBytes(fi.OpenText().ToString().ToCharArray());
textBox1.Text = ba.ToString();
}

private void textBox1_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop))
{
string file = (string)e.Data.GetData(DataFormats.FileDrop);
if (Path.GetExtension(file) != "dat")
{
s = "broken file";
}
}
}


AllowDrop is enabled on both Form1 and textBox1. The events are enabled and start when DragDrop and DragEnter are performed. I've already tried rearranging the code so that textBox1_DragEnter is located before textBox1_DragDrop, but that doesn't work. What is wrong with this code?

Answer Source

You have to set the Effect property in the DragEnter event:

void textBox1_DragEnter(object sender, DragEventArgs e) {
  e.Effect = DragDropEffects.Copy;
  // your code...
}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download