views:

434

answers:

4

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

A: 

Control has various events for dealing with drag/drop - you'll probably only need to look at the DragDrop event for what you want.

Lee
+3  A: 

Hi, try this

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.Effect = DragDropEffects.Copy;
      else
        e.Effect = 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;
    }
  }

Bye.

RRUZ
A: 

Set AllowDrop to true on your TextBox, and write the following code for the DragDrop and DragEnter events :

    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;
        }
    }
Thomas Levesque
A: 

CodeProject has a really nice example of doing this, including how to enable drag and drop both ways (from Explorer to your app, and from your app to Explorer).

Daniel Pryden