拖动文件或文件夹的文本? C#拖动、文件夹、文本、文件

2023-09-05 23:00:25 作者:天蝎座的人会发光

我如何拖动文件或文件夹到一个文本框?我想提出的文件夹名在很文本框。 C#.NET

How do i drag files or folders into a textbox? i want to put the foldername in that very textbox. C# .NET

推荐答案

我写了一个基于在此的链接

i wrote this code based in this link

  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;
    }
  }