c# - 将文件拖放到我的列表框中

标签 c#

我尝试向我的应用程序添加选项以将文件拖动到我的 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

最佳答案

我找到的这段代码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);
}

关于c# - 将文件拖放到我的列表框中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21706747/

相关文章:

c# - 将字符串作为多个参数传递?

c# - 在 WCF SOAP API 中使用 HTTP 授权 header 进行身份验证

c# - 如何在 .NET 应用程序中使用 C++ 项目?

c# - 如何将 powershell 集成到 C sharp

c# - 如何更改 .NET exe 中的字符串? (翻译)

c# - 如何用重复值更新元素值?

c# - 为什么 Byte 被提升为 Enum?

c# - 如何使用 XmlWriter 编写带有十进制样式实体的 XML

c# - 有条件的

c# - ThreadPool 线程执行 I/O 操作 - 线程可以在等待时重用吗?