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.运行结果

相关推荐
小李云雾17 小时前
FastAPI 后端开发:文件上传 + 表单提交
开发语言·python·lua·postman·fastapi
llm大模型算法工程师weng17 小时前
Python敏感词检测方案详解
开发语言·python·c#
fengci.17 小时前
php反序列化(复习)(第二章)
android·开发语言·学习·php
ZHENGZJM17 小时前
后端基石:Go 项目初始化与数据库模型设计
开发语言·数据库·golang
拾贰_C17 小时前
【Claude Code | bash | install】安装Claude Code
开发语言·bash
会编程的土豆17 小时前
【数据结构与算法】堆排序
开发语言·数据结构·c++·算法·leetcode
cch891817 小时前
五大PHP框架对比:如何选择最适合你的?
开发语言·php
南 阳17 小时前
Python从入门到精通day62
开发语言·python
游乐码17 小时前
c#stack
开发语言·c#
cch891817 小时前
Laravel vs 主流PHP框架:终极对决
开发语言·php·laravel