日期:2014-05-18 浏览次数:20939 次
private void textBox1_DragEnter(object sender, DragEventArgs e) { if (e.Data.GetDataPresent(DataFormats.FileDrop)) { e.Effect = DragDropEffects.All; } else { e.Effect = DragDropEffects.None; } } private void textBox1_DragDrop(object sender, DragEventArgs e) { String[] files = (String[])e.Data.GetData(DataFormats.FileDrop, false); if (files.Length > 0) { textBox1.Text = files[0]; } }
------解决方案--------------------
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.textBox1.Multiline = true;
this.textBox1.AllowDrop = true;
this.textBox1.DragDrop+=new DragEventHandler(textBox1_DragDrop);
this.textBox1.DragEnter+=new DragEventHandler(textBox1_DragEnter);
}
protected void textBox1_DragEnter(object sender, System.Windows.Forms.DragEventArgs e)
{
IDataObject dataObject = e.Data;
if (dataObject.GetDataPresent(DataFormats.FileDrop))
{
e.Effect = DragDropEffects.Copy;
}
}
protected void textBox1_DragDrop(object sender, System.Windows.Forms.DragEventArgs e)
{
IDataObject dataObject = e.Data;
if (dataObject == null) return;
if (dataObject.GetDataPresent(DataFormats.FileDrop))
{
string[] files = (string[])dataObject.GetData(DataFormats.FileDrop);
foreach (string file in files)
{
System.IO.FileInfo fi = new System.IO.FileInfo(file);
this.textBox1.Text += "文件名:" + fi.Name;
}
}
}
}