this.richTextBox1.AllowDrop = true;//设置允许拖拽
this.richTextBox1.DragEnter += new DragEventHandler(this.richTextBox1_DragEnter);//注册事件
this.richTextBox1.DragDrop += new DragEventHandler(this.richTextBox1_DragDrop);
private void richTextBox1_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop))
{
e.Effect = DragDropEffects.Link;
}
else
{
e.Effect = DragDropEffects.None;
}
}
private void richTextBox1_DragDrop(object sender, DragEventArgs e)
{
Array arrayfilename = (Array)e.Data.GetData(DataFormats.FileDrop);//有可能直接拖拽很多文件 这里生成一个所有文件全路径的列表
string filename = arrayfilename.GetValue(0).ToString();//选择第一个文件 当然循环可实现加载所有文件
using (StreamReader sr = new StreamReader(filename, System.Text.Encoding.Default))
{
string strred = sr.ReadToEnd().ToString();
richTextBox1.Text = strred;
}
}
2454

被折叠的 条评论
为什么被折叠?



