How do i drag files or folders into a textbox? i want to put the foldername in that very textbox. C# .NET
+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
2009-09-02 23:02:55
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
2009-09-02 23:11:11
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
2009-09-02 23:20:43