vainquit 2018-08-25 18:05 采纳率: 100%
浏览 3136
已采纳

为什么C#的Form设置了AllowDrop=true仍无法把文件拖动到窗口上面

一旦用鼠标拖动文件放到Form上面,鼠标就变成了一个圆圈中间有个斜杠(即代表无法把文件拖进去)
但我确实已经把Form的AllowDrop属性设置为true,
问这个问题的不止我一个,解决方法也都试过(如关闭UAC、如添加DragEnter的方法),均无效。
https://blog.csdn.net/FL1623863129/article/details/77066036?locationNum=6&fps=1
https://blog.csdn.net/aiqinxuancai/article/details/50903564
http://tieba.baidu.com/p/3059784450
https://zhidao.baidu.com/question/515025128.html

实在不知道哪里出了问题。
可在这里下载源文件,很简单的几段代码(从网上抄来的):
https://pan.baidu.com/s/1A6ag0xpB3nmyP4JG2UmvXQ

(另外,该代码中有个按钮和文字框,无视里面的内容就好)

以下有简易的代码可供浏览。但我觉得可能不只是代码本身的问题?

 public Form1()
        {
            InitializeComponent();
            this.AllowDrop = true;

        }
        private void Form1_Load(object sender, EventArgs e)
        {
            this.AllowDrop = true;
        }

  private void Form1_DragEnter(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.FileDrop))
            {
                //e.Effect = DragDropEffects.Move;
                //lb1.Text = "12345";
                if (e.Data.GetDataPresent(DataFormats.FileDrop)) e.Effect = DragDropEffects.Link;
                else e.Effect = DragDropEffects.None;
            }
        }
  • 写回答

3条回答 默认 最新

  • threenewbee 2018-08-26 06:07
    关注
     using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    
    namespace Q698507
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            private void Form1_DragDrop(object sender, DragEventArgs e)
            {
                if (e.Data.GetDataPresent(DataFormats.FileDrop))
                {
                    label1.Text = ((string[])e.Data.GetData(DataFormats.FileDrop.ToString()))[0];
                }
            }
    
            private void Form1_DragOver(object sender, DragEventArgs e)
            {
                e.Effect = DragDropEffects.Copy;
            }
        }
    }
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料