拖放不适用于C#Winforms Application

时间:2014-10-29 10:53:59

标签: c# winforms drag-and-drop

我正在尝试创建一个可以删除文件/文件夹的Windows窗体。

我在WinForms应用程序中有以下代码

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

    private void Form1_DragEnter(object sender, DragEventArgs e)
    {
        Debug.Print("DragEnter");
    }

    private void Form1_DragDrop(object sender, DragEventArgs e)
    {
        MessageBox.Show("Dropped!");
    }
}

我已将AllowDrop属性设置为true。 我已尝试在Visual Studio中的调试中运行该应用程序。 根据其他类似问题的答案,我尝试以管理员身份运行已编译的exe。 我尝试以管理员身份运行已编译的exe 而不是

但无论我做什么,我都无法触发DragDrop事件。但是,DragEnter事件触发。我错过了什么?

8 个答案:

答案 0 :(得分:22)

您的DragDropEffect设置得恰当吗?尝试将其放在DragEnter事件处理程序方法中:

    private void Form1_DragEnter(object sender, DragEventArgs e)
    {
        Console.WriteLine("DragEnter!");
        e.Effect = DragDropEffects.Copy;
    }

默认设置为DragDropEffects.None,因此Drop事件不会触发。

答案 1 :(得分:9)

对于那些会阅读此内容的人,因为上述提示不起作用。

请注意,如果您运行Visual Studio或您的应用程序"作为管理员"那么Drag& Drop将无法正常工作如此处所述:https://visualstudio.uservoice.com/forums/121579-visual-studio-ide/suggestions/2164233-fix-drag-and-drop-to-open-file-when-running-as-adm

答案 2 :(得分:4)

尝试在Form1_DragEnter中使用类似的东西:

private void Form1_DragEnter(object sender, DragEventArgs e)
{
    if (e.Data.GetDataPresent(DataFormats.FileDrop))
        e.Effect = DragDropEffects.All;
    else
    {
        String[] strGetFormats = e.Data.GetFormats();
        e.Effect = DragDropEffects.None;
    }
}

这会触发你的Form1_DragDrop

答案 3 :(得分:4)

不要忘记在表单的属性中将 AllowDrop 更改为" True"你的代码可能没什么问题但是如果这个属性没有启用,那么它就不会工作了。默认设置为false。

答案 4 :(得分:0)

您是否编写过拖动对象的MouseDown和MouseMove事件。

答案 5 :(得分:0)

另一个非常棘手且棘手的问题可能是您已覆盖OnHandleCreated,但忘记调用基本实现。然后,您的应用程序无法设置所需的内部窗口设置以尊重您的AllowDrop属性。

,请务必在覆盖中拨打base.OnHandleCreated(e),然后就可以了。

答案 6 :(得分:0)

尽管将AllowDrop设置为true,我也遇到了这个令人困惑的问题!

在我的Windows窗体应用程序(VS2017)中,我必须确保设置了一个有效的启动对象:例如myprojectname.Program,一切都很好!

答案 7 :(得分:0)

我指定了一个命令行,该命令行指向不再存在的文件。不知何故阻止了阻力进入射击。一旦删除它,一切就再好了。

相关问题