C# 文件拖入控件中,显示文件路径

1.设置所需拖入的控件(以Textbox为列)属性为:

cs 复制代码
this.textBox1.AllowDrop = true; //设置AllowDrop 属性为 true,使之支持拖拽,否则拖拽显示禁用状态

2.设置该控件的两个事件,分别为:

①DragEnter 在用鼠标将某项拖动到该控件的工作区时发生。

②DragDrop 拖放操作完成时发生。

3.编写事件后台代码

cs 复制代码
  private void textBox1_DragEnter(object sender, DragEventArgs e)
  {
      if (e.Data.GetDataPresent(DataFormats.FileDrop))
      {
          e.Effect = DragDropEffects.Link;
      }

      else
      {
          e.Effect = DragDropEffects.None;
      }
  }

  private void textBox1_DragDrop(object sender, DragEventArgs e)
  {
      string path = ((System.Array)e.Data.GetData(DataFormats.FileDrop)).GetValue(0).ToString();
      textBox1.Text = path;
  }

4.运行结果

相关推荐
小白学大数据13 分钟前
爬虫性能天花板:asyncio赋能 Aiohttp,并发提速 10 倍
开发语言·爬虫·数据分析
凡人叶枫23 分钟前
Effective C++ 条款07:为多态基类声明 virtual 析构函数
linux·c语言·开发语言·c++
凡人叶枫33 分钟前
Effective C++ 条款10:令 operator= 返回一个 reference to *this
java·linux·服务器·开发语言·c++·effective c++
leo__5201 小时前
MATLAB实现牧羊人算法
开发语言·算法·matlab
格发许可优化管理系统1 小时前
Mentor许可证使用规定全解析
java·大数据·c语言·开发语言·c++
Popeye-lxw1 小时前
由罗技 K380 键盘 FN 键模式切换引发的血案
c#
FL16238631291 小时前
C# OpenCvSharp 基于霍夫变换直线检测的文本图像倾斜校正文本图像倾斜校
开发语言·c#
techdashen2 小时前
在 Fly.io 上使用 Rust 构建远程开发环境:从 Tokio 到 eBPF
开发语言·后端·rust
留白_2 小时前
pandas文件读取与存储
开发语言·python·pandas
夕除2 小时前
AOP 实现 Redis 缓存切面解析
java·开发语言·python