首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >是否在文本框中拖动文件或文件夹?C#

是否在文本框中拖动文件或文件夹?C#
EN

Stack Overflow用户
提问于 2009-09-02 22:45:29
回答 5查看 15.6K关注 0票数 7

如何将文件或文件夹拖动到文本框中?我想把文件夹的名字放在那个文本框里。C# .NET

EN

回答 5

Stack Overflow用户

回答已采纳

发布于 2009-09-02 23:02:55

我基于这个link写了这段代码

代码语言:javascript
运行
复制
  public partial class Form1 : Form
  {
    public Form1()
    {
      InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
      textBox1.AllowDrop = true;
      textBox1.DragEnter += new DragEventHandler(textBox1_DragEnter);
      textBox1.DragDrop += new DragEventHandler(textBox1_DragDrop);

    }

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

    private void textBox1_DragDrop(object sender, DragEventArgs e)
    {
      string[] FileList = (string[])e.Data.GetData(DataFormats.FileDrop, false);


    string s="";

    foreach (string File in FileList)
    s = s+ " "+ File ;
    textBox1.Text = s;
    }
  }
票数 18
EN

Stack Overflow用户

发布于 2009-09-02 23:11:12

在TextBox上将AllowDrop设置为true,并为DragDrop和DragEnter事件编写以下代码:

代码语言:javascript
运行
复制
    private void textBox1_DragEnter(object sender, DragEventArgs e)
    {
        if (e.Data.GetDataPresent(DataFormats.FileDrop))
        {
            e.Effect = DragDropEffects.Copy;
        }
    }

    private void textBox1_DragDrop(object sender, DragEventArgs e)
    {
        if (e.Data.GetDataPresent(DataFormats.FileDrop))
        {
            string[] fileNames = (string[])e.Data.GetData(DataFormats.FileDrop);
            textBox1.Lines = fileNames;
        }
    }
票数 5
EN

Stack Overflow用户

发布于 2009-09-02 23:20:43

这样做的CodeProject has a really nice example,包括如何启用双向拖放(从资源管理器到你的应用程序,从你的应用程序到资源管理器)。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/1370538

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档