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条)

报告相同问题?

悬赏问题

  • ¥35 平滑拟合曲线该如何生成
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了
  • ¥15 链式存储应该如何解决
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站