• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录

gisoracle

  • 博客园
  • 联系
  • 订阅
  • 管理

公告

View Post

文件拖放到WinForm控件上,文件途径(地址)显示到控件上

先看一下效果,在这里我以TextBox控件为例,其它类型的操作也类似于这样

23.gif

视频讲解地址 https://www.bilibili.com/video/BV1AV4y1M7mR

步骤如下(控件名为textBox1)

1、注册两个事件,代码如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
private void textBox1_DragDrop(object sender, DragEventArgs e)
{
    textBox1.Text = ((System.Array)e.Data.GetData(DataFormats.FileDrop)).GetValue(0).ToString();
}
 
private void textBox1_DragEnter(object sender, DragEventArgs e)
{
    //只允许文件拖放
    if (e.Data.GetDataPresent(DataFormats.FileDrop))
    {
        e.Effect = DragDropEffects.Copy;
    }
    else
    {
        e.Effect = DragDropEffects.None;
    }
}

2、设置控件属性

1
textBox1.AllowDrop = true;

转载请保留 http://www.luofenming.com/show.aspx?id=dbad2a805bc4453698dfdae8aa841bbd

posted on 2022-09-15 08:08  gisai  阅读(131)  评论(0)    收藏  举报

刷新页面返回顶部
 
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3