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/

posted @   opencoder  阅读(258)  评论(0编辑  收藏  举报
编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· DeepSeek 开源周回顾「GitHub 热点速览」
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
点击右上角即可分享
微信分享提示