【问题标题】:File drag and drop not working on listbox文件拖放在列表框上不起作用
【发布时间】:2015-01-27 06:27:03
【问题描述】:

这是我第一次使用拖放。所以我有一个带有listbox 的表格,没有别的。我希望能够将文件从桌面或 Windows 资源管理器拖放到我的列表框中。这是我的代码。缺少什么?

表格:

 public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void listBox1_DragEnter(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.FileDrop))
                e.Effect = DragDropEffects.All;
            else
                e.Effect = DragDropEffects.None;
        }


        private void listBox1_DragDrop(object sender, DragEventArgs e)
        {
            string[] s = (string[])e.Data.GetData(DataFormats.FileDrop, false);
            int i;
            for (i = 0; i < s.Length; i++)
                listBox1.Items.Add(s[i]);
        }
    }

Form1.Designer.cs:(初始化组件)

private void InitializeComponent()
{
    this.listBox1 = new System.Windows.Forms.ListBox();
    this.SuspendLayout();
    // 
    // listBox1
    // 
    this.listBox1.AllowDrop = true;
    this.listBox1.FormattingEnabled = true;
    this.listBox1.Location = new System.Drawing.Point(30, 23);
    this.listBox1.Name = "listBox1";
    this.listBox1.Size = new System.Drawing.Size(376, 238);
    this.listBox1.TabIndex = 0;
    this.listBox1.DragDrop += new System.Windows.Forms.DragEventHandler(this.listBox1_DragDrop);
    this.listBox1.DragEnter += new System.Windows.Forms.DragEventHandler(this.listBox1_DragEnter);
    this.listBox1.DragOver += new System.Windows.Forms.DragEventHandler(this.listBox1_DragOver);
    // 
    // Form1
    // 
    this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
    this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
    this.ClientSize = new System.Drawing.Size(438, 366);
    this.Controls.Add(this.listBox1);
    this.Name = "Form1";
    this.Text = "Form1";
    this.ResumeLayout(false);
}

【问题讨论】:

  • 你在 DragOver 事件中有什么?
  • 具体说明你想要的效果,只有 DragDropEffects.Copy 有意义。并删除 DragOver 事件处理程序。
  • @HansPassant 我希望将文件名添加到我的列表框中。我还删除了DragOverevent 处理程序。
  • 好吧,调试器告诉你什么?如果您根本没有收到 DragEnter 事件,那么您需要在没有提升的情况下运行 Visual Studio。
  • Windows 不允许从非提升应用拖放到提升应用。 UIPI 禁止它,这是 UAC 鲜为人知的方面。

标签: c# winforms visual-studio drag-and-drop listbox


【解决方案1】:

我做了这个,我认为这没问题。不需要 DragOver。

    private void listBox_DragEnter(object sender, DragEventArgs e)
    {
        if (e.Data.GetDataPresent(DataFormats.FileDrop))
            e.Effect = DragDropEffects.All;
        else
            e.Effect = DragDropEffects.None;
    }

    private void listBox_DragDrop(object sender, DragEventArgs e)
    {
        if (listBox.Items.Count != 0)
        {
            listBox.Items.Clear();
        }
        string[] s = (string[])e.Data.GetData(DataFormats.FileDrop, false);
        int i;
        for (i = 0; i < s.Length; i++)
            listBox.Items.Add(Path.GetFileName(s[i]));
    }

【讨论】:

  • 什么都没有改变。当我将文件拖入listbox 时,我仍然会看到“不允许”图标。
  • 我的表单是否也应该将其AllowDrop 选项设置为true
  • AllowDrop 是真的。我发现了一件事,像回收站这样的图标做不到。
  • 我不这么认为,但我不确定。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-04-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-05-29
  • 1970-01-01
相关资源
最近更新 更多