拖放在C#Winforms应用程序中不起作用 [英] Drag and Drop not working in C# Winforms Application

查看:130
本文介绍了拖放在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应用程序中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆