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

为什么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 kali环境运行volatility分析android内存文件,缺profile
  • ¥15 写uniapp时遇到的问题
  • ¥15 vs 2008 安装遇到问题
  • ¥15 matlab有限元法求解梁带有若干弹簧质量系统的固有频率
  • ¥15 找一个网络防御专家,外包的
  • ¥100 能不能让两张不同的图片md5值一样,(有尝)
  • ¥15 informer代码训练自己的数据集,改参数怎么改
  • ¥15 请看一下,学校实验要求,我需要具体代码
  • ¥50 pc微信3.6.0.18不能登陆 有偿解决问题
  • ¥20 MATLAB绘制两隐函数曲面的交线