实现原理:使用扩展方法。

    /// <summary>
/// 控件扩展
/// </summary>
public static class ControlExt
{
/// <summary>
/// 文件拖拽支持
/// </summary>
/// <param name="control"></param>
/// <param name="FileNamesCallBack">拖拽的文件路径列表</param>
public static void ControlUseDrop(this Control control,Action<List<String>> FileNamesCallBack)
{
control.AllowDrop = true;//设置控件运行拖放
control.DragEnter += Control_DragEnter;
control.DragDrop += (sender,e)=> {
Array array = e.Data.GetData(DataFormats.FileDrop) as Array;
List<String> fileNames = new List<string>();
foreach (Object item in array)
{
fileNames.Add(item.ToString());
}
FileNamesCallBack?.Invoke(fileNames);
};
}private static void Control_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop))
{
e.Effect = DragDropEffects.Link;
}
else
{
e.Effect = DragDropEffects.None;
}
}
}

使用方法:在窗体的load事件或构造方法中:

  this.txt_selectFiles.ControlUseDrop(files => {
fileNames=files.Where(f => f.EndsWith(".sql")).ToList();
this.txt_selectFiles.Text = string.Join("\r\n", fileNames);
});

扩展方法是.net提供的一个语法糖,使用它可以很方便的封装一些通用功能。

让您的WinForm控件快速支持拖拽文件的更多相关文章

  1. 让一个view 或者控件不支持拖拽

    让一个view 或者控件不支持拖拽: dragView.userInteractionEnabled = NO;

  2. jqGrid选择列控件向右拖拽超出边界处理

    jqGrid选择列控件向右拖拽超出边界处理 $("#tb_DeviceInfo").jqGrid('navButtonAdd', '#jqGridPager', {         ...

  3. WPF 在image控件用鼠标拖拽出矩形

    原文:WPF 在image控件用鼠标拖拽出矩形 版权声明:博客已迁移到 http://lindexi.gitee.io 欢迎访问.如果当前博客图片看不到,请到 http://lindexi.gitee ...

  4. 2018-11-19-WPF-在image控件用鼠标拖拽出矩形

    title author date CreateTime categories WPF 在image控件用鼠标拖拽出矩形 lindexi 2018-11-19 15:35:13 +0800 2018- ...

  5. 关于安装了VMware tools后仍然不支持拖拽文件的问题

    我在学校机房里面的redhat4上面安装了VMware tools之后能正常支持拖拽,但是我自己电脑上的却不支持,折腾了好久,网上找了很久也还是没有解决,不过发现了一些问题,总结如下:(当然我总结的这 ...

  6. Winform中Picture控件图片的拖拽显示

    注解:最近做了一个小工具,在Winform中对Picture控件有一个需求,可以通过鼠标从外部拖拽图片到控件的上,释放鼠标,显示图片! 首先你需要对你的整个Fom窗口的AllowDrop设置Ture ...

  7. ios-将代码创建的视图控件放入拖拽控件的下面

    如图所示 图片是拖拽上去的imageView,橘黄色控件是在代码中创建的添加上去的,此时黄色view在imageView 上方 调用方法bringSubviewToFront:试图将imageView ...

  8. win32 sdk树形控件的项拖拽实现

    本课中,我们将学习如何使用树型视图控件.另外还要学习如何在树型视图中完成拖-拉动作,以及如何使用图象列表. 理论: 树型视图是一种特别的窗口,我们可以使用它一目了然地表示某种层次关系.譬如象在资源管理 ...

  9. devpress 的gridview 控件的行拖拽 z

    首先,添加引用:using DevExpress.XtraGrid.Views.Grid.ViewInfo;               gridControl1.AllowDrop = true; ...

随机推荐

  1. JavaScript实现的图片循环播放

    直接上干货 <html> <head> <title>Banner Cycler</title> <script> var banners ...

  2. Invitation Cards POJ - 1511

    题目链接:https://vjudge.net/problem/POJ-1511 思路:题目意思就是,从1出发到所有城市,再从所有城市回到1的最短时间. 那么我们只要正跑一次图,然后反向存边,再跑一次 ...

  3. @TableField(select=false)

    使用这个注解排除删除标识字段.

  4. swarm 集群

    1.创建manage节点 $docker swarm init --advertise-addr=192.168.0.38(主机ip) To add a worker to this swarm, r ...

  5. day31_8.12并发编程二线程

    一.进程间的通信 在进程中,可以通过Queue队列进行通信,队列有两个特点: 1.先进先出.(先来的数据先被取出) 2.管道式存取.(数据取出一次后就不会在有了) 在python中有以下方法来操作数据 ...

  6. 【转】为什么要重写hashcode()方法和toString()方法

    Object 类 包含toString()和hashCode()方法. 一.toString(): 在Object类里面定义toString()方法的时候返回的对象的哈希code码,这个hashcod ...

  7. [LeetCode] 924. Minimize Malware Spread 最大程度上减少恶意软件的传播

    In a network of nodes, each node i is directly connected to another node j if and only if graph[i][j ...

  8. [LeetCode] 172. Factorial Trailing Zeroes 求阶乘末尾零的个数

    Given an integer n, return the number of trailing zeroes in n!. Example 1: Input: 3 Output: 0 Explan ...

  9. 区间DP(超详细!!!)

    一.问题 给定长为n的序列a[i],每次可以将连续一段回文序列消去,消去后左右两边会接到一起,求最少消几次能消完整个序列,n≤500. f[i][j]表示消去区间[i,j]需要的最少次数. 则; 若a ...

  10. Elasticsearch由浅入深(七)搜索引擎:_search含义、_multi-index搜索模式、分页搜索以及深分页性能问题、query string search语法以及_all metadata原理

    _search含义 _search查询返回结果数据含义分析 GET _search { , "timed_out": false, "_shards": { , ...