【问题标题】:Drag & Drop Shows Circle With Line Through It拖放显示带有线的圆圈
【发布时间】:2015-04-10 13:25:15
【问题描述】:

我认为我的设置很准确,但是每当我尝试将文件拖到我的表单上时,它只会显示一个带有一条线的圆圈。哪个部分设置不正确?

private void Form1(object sender, EventArgs e)
{
  this.AllowDrop = true;
  this.DragDrop += new DragEventHandler(Form1_DragDrop);
  this.DragEnter += new DragEventHandler(Form1_DragEnter);
}
private void Form1_DragDrop(object sender, DragEventArgs e)
{
  if (!e.Data.GetDataPresent(DataFormats.FileDrop)) return;
  string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);
  foreach (var file in files)
  {
    var ext = Path.GetExtension(file);
    if (ext.Equals(".xlsx", StringComparison.CurrentCultureIgnoreCase))
    {
        e.Effect = DragDropEffects.Copy;
        MessageBox.Show(sender.ToString());
        return;
    }
    else { MessageBox.Show("This filetype is not allowed");
  }
}
private void Form1_DragEnter(object sender, DragEventArgs e)
{
  if (!e.Data.GetDataPresent(DataFormats.FileDrop)) return;
  string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);
  foreach (var file in files)
  {
    var ext = Path.GetExtension(file);
    if (ext.Equals(".xlsx", StringComparison.CurrentCultureIgnoreCase))
    {
        e.Effect = DragDropEffects.Copy;
        MessageBox.Show(sender.ToString());
        return;
    }
    else { MessageBox.Show("This filetype is not allowed");
  }
}

编辑代码

【问题讨论】:

  • 响应 DragEnter 以测试您是否可以接受该数据类型并设置提示
  • @Plutonix 添加了那个事件处理程序,结果还是一样?见编辑。
  • 您需要在 DragEventArgs 中设置允许的内容
  • 你忘记了最后一部分:set the cue.
  • @JamesBarrass -- 查看编辑,结果还是一样

标签: c# winforms drag-and-drop


【解决方案1】:

这对我有用...

public partial class Form1 : Form
{
    public Form1()
    {
        this.InitializeComponent();
        this.AllowDrop = true;
        this.DragDrop += new DragEventHandler(Form1_DragDrop);
        this.DragEnter += new DragEventHandler(Form1_DragEnter);
    }
    private void Form1_DragDrop(object sender, DragEventArgs e)
    {
        if (e.Data.GetDataPresent(DataFormats.FileDrop))
        {
            string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);
            foreach (var file in files)
            {
                MessageBox.Show(file);
            }
        }
    }

    private void Form1_DragEnter(object sender, DragEventArgs e)
    {
        if (e.Data.GetDataPresent(DataFormats.FileDrop))
        {
            e.Effect = DragDropEffects.Copy;
        }
    }
}

从 Windows 资源管理器/我的桌面等中删除任何文件会显示一个带有路径和文件名的消息框

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-08-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-06
    • 1970-01-01
    • 2022-08-19
    • 1970-01-01
    相关资源
    最近更新 更多