【问题标题】:Drag and drop files into my listbox将文件拖放到我的列表框中
【发布时间】:2014-02-11 15:58:40
【问题描述】:

我尝试将选项添加到我的应用程序中,以将文件拖到我的Listbox 中,而不是导航到文件夹中,这就是我尝试过的:

private void Form1_Load(object sender, EventArgs e)
{
    listBoxFiles.AllowDrop = true;
    listBoxFiles.DragDrop += listBoxFiles_DragDrop;
    listBoxFiles.DragEnter += listBoxFiles_DragEnter;
}

private void listBoxFiles_DragEnter(object sender, DragEventArgs e)
{
    e.Effect = DragDropEffects.Copy;
}

private void listBoxFiles_DragDrop(object sender, DragEventArgs e)
{
    listBoxFiles.Items.Add(e.Data.ToString());
}

但不是完整的文件路径e.Data.ToString()return System.Windows.Forms.DataObject

【问题讨论】:

    标签: c#


    【解决方案1】:

    我找到的这段代码here

    private void Form1_Load(object sender, EventArgs e)
    {
        listBoxFiles.AllowDrop = true;
        listBoxFiles.DragDrop += listBoxFiles_DragDrop;
        listBoxFiles.DragEnter += listBoxFiles_DragEnter;
    }
    
    private void listBoxFiles_DragEnter(object sender, DragEventArgs e)
    {
        if (e.Data.GetDataPresent(DataFormats.FileDrop)) e.Effect = DragDropEffects.Copy;
    }
    
    private void listBoxFiles_DragDrop(object sender, DragEventArgs e)
    {
        string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);
        foreach (string file in files)
            listBoxFiles.Items.Add(file);
    }
    

    【讨论】:

      【解决方案2】:

      我尝试在 WPF 中使用@AsfK 的答案,不得不删除

      listBoxFiles.DragDrop += listBoxFiles_DragDrop;
      

      来自public MainWindow(),否则我复制了拖动的文件。

      谢谢!

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多