Drag and drop folder to a TextBox in C#

Last evening I came up with a requirement to drag a folder path into a TextBox in one of my Windows form applications. So I just want to share it with you all through a simple example.

Drag-and-drop operations in WinForms accomplish via handling of a series of events, mostly with the DragDrop, DragEnter and DragLeave. Working with the event arguments of these events eventually you can facilitate drag and drop.

How to perform a drag & drop

Set the property called AllowDrop to true on the selected control. (a TextBox in this case)

The DragEnter event for the control where the drop will occur, do the type-checking here to ensure that the data being dragged is of an acceptable type. In this case it is FileDrop, which specifies the Windows file drop format.

private void txtFolderPath_DragEnter(object sender, DragEventArgs e)
{
     if (e.Data.GetDataPresent(DataFormats.FileDrop))
     {
          e.Effect = DragDropEffects.Copy;
     }
     else
     {
          e.Effect = DragDropEffects.None;
     }
}

The DragDrop event for the control where the drop will occur, retrieve the data being dragged using the GetData.

private void txtFolderPath_DragDrop(object sender, DragEventArgs e)
{
     if (e.Data.GetDataPresent(DataFormats.FileDrop))
     {
          string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);

          if (Directory.Exists(files[0]))
          {
               this.txtFolderPath.Text = files[0];
          }
     }
}

In the complete code below, a TextBox control is the control being the drop will occur. Once you drag a folder into the TextBox and drop, the relevant folder path is added into the TextBox text property and display.

public partial class Form1 : Form
{
     private System.Windows.Forms.TextBox txtFolderPath = new TextBox();

     public Form1()
     {
          InitializeComponent();

          this.txtFolderPath.Location = new System.Drawing.Point(20, 30);
          this.txtFolderPath.Size = new System.Drawing.Size(200, 20);
          this.Controls.Add(this.txtFolderPath);

          this.txtFolderPath.AllowDrop = true;
          this.txtFolderPath.DragEnter += new DragEventHandler(txtFolderPath_DragEnter);
          this.txtFolderPath.DragDrop += new DragEventHandler(txtFolderPath_DragDrop);
     }

     private void txtFolderPath_DragDrop(object sender, DragEventArgs e)
     {
          if (e.Data.GetDataPresent(DataFormats.FileDrop))
          {
               string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);

               if (Directory.Exists(files[0]))
               {
                    this.txtFolderPath.Text = files[0];
               }
          }
     }

     private void txtFolderPath_DragEnter(object sender, DragEventArgs e)
     {
          if (e.Data.GetDataPresent(DataFormats.FileDrop))
          {
               e.Effect = DragDropEffects.Copy;
          }
          else
          {
               e.Effect = DragDropEffects.None;
          }
     }
}

Ref: https://erangatennakoon.wordpress.com/2012/04/10/drag-and-drop-folder-to-a-textbox-in-c/

原文地址:https://www.cnblogs.com/open-coder/p/12661797.html

时间: 2024-10-10 18:35:47

Drag and drop folder to a TextBox in C#的相关文章

原生拖拽,拖放事件(drag and drop)

原生拖拽,拖放事件(drag and drop) 拖拽,拖放事件可以通过拖拽实现数据传递,达到良好的交互效果,如:从操作系统拖拽文件实现文件选择,拖拽实现元素布局的修改. drag and drop事件流程 一个完整的drag and drop流程通常包含以下几个步骤: 设置可拖拽目标.设置属性draggable="true"实现元素的可拖拽. 监听dragstart设置拖拽数据 为拖拽操作设置反馈图标(可选) 设置允许的拖放效果,如copy,move,link 设置拖放目标,默认情况

[Javascript + rxjs] Simple drag and drop with Observables

Armed with the map and concatAll functions, we can create fairly complex interactions in a simple way. We will use Observable to create a simple drag and drop example with basic DOM elements. <!DOCTYPE html> <html> <head lang="en"

关于QListWidget的Drag和Drop用法

QListWIdget的Drag和Drop. 真是坑爹到不行... 其实很简单的.. 最后搞到好多行代码.. 真是作死的节奏............... 先上代码:<这种方法也可以实现, 但是太麻烦了...> #include "PlayerDragWidget.h" #include "PlayerLyricsListItem.h" #include <QDrag> #include <QPainter> #include &

基于HTML5的Drag and Drop生成图片Base64信息

直击现场 基于HTML5的Drag and Drop生成图片Base64信息 发表于4个月前(2014-12-19 00:58)   阅读(103) | 评论(0) 11人收藏此文章, 我要收藏 赞0 慕课网,程序员升职加薪神器,点击免费学习 摘要 HTML5的Drag and Drop是很不错的功能,网上使用例子较多如 http://html5demos.com/drag ,但这些例子大部分没实际用途,本文将搞个有点使用价值的例子,通过Drag and Drop生成图片的Base64的字符串信

drag and drop

<!DOCTYPE HTML> <html> <head> <script type="text/javascript"> function allowDrop(ev) { ev.preventDefault(); } function drag(ev) { ev.dataTransfer.setData("Text",ev.target.id); } function drop(ev) { ev.preventDef

delphi Drag and Drop sample 鼠标拖放操作实例

Drag and Drop is a common operation that makes the interface user friendly: a user can drag/drop information to controls instead of having to type etc. The following sample explains basics of drag and drop. For detailed information you should refer t

通过HTML5的Drag and Drop生成拓扑图片Base64信息

HTML5 原生的 Drag and Drop是很不错的功能,网上使用例子较多如 http://html5demos.com/drag ,但这些例子大部分没实际用途,本文将搞个有点使用价值的例子,通过Drag and Drop生成图片的Base64的字符串信息. 使用Base64方式的图片有诸多好处,可将多个图片信息整合到单个js文件避免多次http请求,可以避免WebGL例子跨域访问的安全限制无法本地文件运行等好处,当然弊端也不少例如不能有效利用浏览器图片缓存机制等.使用HT for Web的

Android 用户界面---拖放(Drag and Drop)(三)

设计拖放操作 本节主要内容如下: 1.  如何开始拖拽: 2.  在拖拽期间如何响应事件: 3.  如何响应落下事件: 4.  如何结束拖放操作. 开始拖拽 用户使用一个拖拽手势开始拖拽,通常是在View对象上长按.在响应中,应该做下列事情: 1.  必要时,给要移动的数据创建一个ClipData和ClipData.Item对象,作为ClipData对象的一部分,在ClipData对象内部的ClipDescription对象中保存了元数据.因为拖放操作不代表数据的移动,因此可以使用null来代替

Android:Drag and Drop的应用

最近看了下Drag and Drop部分的原文,觉得很有意思就像自己试着做一下,说实在的原文真的是不好读啊,要感谢那些为我们发表译文的大神们, 真的是不容易,原文中给了例子,但是只有后面零星的代码,真的是不知道怎么用,索性就搜了下原文翻译,顺便看看有没有人实现出一个例子什 么的,只可惜译文多得很就是没人做出一个可以参照的例子,无奈自己就根据文章意思改了改代码,终于是能用了,但是不知道用的对不对,在这 分享一下,也想看看有没有懂的大神给指点指点. 我的理解就是有一个图片通过监听长期的点击事件得到一