拖放在 C# Winforms 应用程序中不起作用

Drag and Drop not working in C# Winforms Application(拖放在 C# Winforms 应用程序中不起作用)
本文介绍了拖放在 C# Winforms 应用程序中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个可以将文件/文件夹拖放到其上的 Windows 窗体.

I am trying to create a windows form onto which I can drop a file/folder.

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

I have the following code in a WinForms app

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 not.

I have set the AllowDrop property to true. I've tried running the application in debug within Visual Studio. Based on answers to other similar questions, I've tried running the compiled exe as administrator. I've tried running the compiled exe not as administrator.

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

But whatever I do, I cannot get the DragDrop event to fire. The DragEnter event does fire, however. What am I missing?

推荐答案

你的 DragDropEffect 设置合适吗?尝试将它放在 DragEnter 事件处理方法中:

Is your DragDropEffect set appropriately? Try placing this in the DragEnter Event Handler Method:

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

默认情况下,它设置为 DragDropEffects.None,因此 Drop 事件不会触发.

By default it was set to DragDropEffects.None so the Drop event wouldn't fire.

这篇关于拖放在 C# Winforms 应用程序中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

DispatcherQueue null when trying to update Ui property in ViewModel(尝试更新ViewModel中的Ui属性时DispatcherQueue为空)
Drawing over all windows on multiple monitors(在多个监视器上绘制所有窗口)
Programmatically show the desktop(以编程方式显示桌面)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
LINQ many-to-many relationship, how to write a correct WHERE clause?(LINQ多对多关系,如何写一个正确的WHERE子句?)