【问题标题】:How to drag & drop only one file on form window如何在表单窗口中仅拖放一个文件
【发布时间】:2014-11-05 08:43:57
【问题描述】:

我在c#中有一个windows应用程序,我想在这个应用程序中添加拖放功能,添加此功能后需要多个文件,所以我想一次只获取一个文件,怎么做?

【问题讨论】:

  • 你试过了吗?
  • 我是 C# 语言的新手,所以我不知道任何方法
  • 你添加了什么吗? “添加此功能后”表示您已完成拖放操作并希望将其限制为一个文件?
  • 是的,我想要一个文件。

标签: c# .net winforms drag-and-drop


【解决方案1】:

阅读下面的代码并尝试将其应用于您的情况

public Form1()
{
    InitializeComponent();
    this.AllowDrop = true;
    this.DragEnter += Form1_DragEnter;
    this.DragDrop += Form1_DragDrop;
}

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

void Form1_DragDrop(object sender, DragEventArgs e)
{
    var files = (string[])e.Data.GetData(DataFormats.FileDrop);
    if (files.Length == 1)
    {
        // do what you want
    }
    else
    {
        // show error
    }
}

【讨论】:

    猜你喜欢
    • 2011-01-05
    • 2013-11-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多