this.richTextBox1.DragEnter += new System.Windows.Forms.DragEventHandler(this.richTextBox1_DragEnter);
this.richTextBox1.DragDrop += new System.Windows.Forms.DragEventHandler(this.richTextBox1_DragDrop);
private void Form1_Load(object sender, EventArgs e)
{//允许richTextBox1控件实现拖放操作
this.richTextBox1.AllowDrop = true;
}
private void richTextBox1_DragEnter(object sender, DragEventArgs e)
{//从拖动源复制数据,并将其滚动到放置目标中
if(e.Data.GetDataPresent(DataFormats.FileDrop))
e.Effect=DragDropEffects.All;
}
private void richTextBox1_DragDrop(object sender, DragEventArgs e)
{//取得文本文件名并打开
//可以使用DataFormats.FileDrop在拖放操作过程中与外壳程序文件的拖动操作进行交互
if(e.Data.GetDataPresent(DataFormats.FileDrop))
{
string[] MyPathArray =(string[])e.Data.GetData(DataFormats.FileDrop) ;
foreach (string MyPath in MyPathArray)
{
this.richTextBox1.LoadFile(MyPath, RichTextBoxStreamType.PlainText);
}
}
}