为什么DragDrop启用的文本框不接受文件?

时间:2017-07-26 23:21:02

标签: c# winforms textbox

Form1和textBox1上都启用了AllowDrop。事件已启用,并在执行DragDrop和DragEnter时启动。我已经尝试重新排列代码,以便textBox1_DragEnter位于textBox1_DragDrop之前,但这并不起作用。这段代码有什么问题?

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";
        }
    }
}

1 个答案:

答案 0 :(得分:0)

您必须在DragEnter事件中设置Effect属性:

void textBox1_DragEnter(object sender, DragEventArgs e) {
  e.Effect = DragDropEffects.Copy;
  // your code...
}