1个回答
展开全部
假定有一个RichTextBox叫richTextBox1
窗体构造函数中:
richTextBox1.AllowDrop = true;
DragEventHandler rtba1,rtba2;
rtba1 = new DragEventHandler(richTextBox1_DragEnter);
rtba2=new DragEventHandler(richTextBox1_DragDrop);
richTextBox1.DragEnter +=rtba1;
richTextBox1.DragDrop += rtba2;
窗体所在类中添加:
void richTextBox1_DragDrop(object sender, DragEventArgs e)
{
String Path = ((System.Array)e.Data.GetData(DataFormats.FileDrop)).GetValue(0).ToString();
FileInfo fi = new FileInfo(Path);
String str = fi.Extension.ToUpper();
if (str == ".TXT")
{
RichTextBox rt = sender as RichTextBox;
int vv = rt.SelectionStart;
rt.Text = rt.Text.Insert(vv, File.ReadAllText(fi.FullName));
rt.SelectionStart = vv;
}
}
void richTextBox1_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop))
{
e.Effect = DragDropEffects.Copy;
}
else
e.Effect = DragDropEffects.None;
}
窗体构造函数中:
richTextBox1.AllowDrop = true;
DragEventHandler rtba1,rtba2;
rtba1 = new DragEventHandler(richTextBox1_DragEnter);
rtba2=new DragEventHandler(richTextBox1_DragDrop);
richTextBox1.DragEnter +=rtba1;
richTextBox1.DragDrop += rtba2;
窗体所在类中添加:
void richTextBox1_DragDrop(object sender, DragEventArgs e)
{
String Path = ((System.Array)e.Data.GetData(DataFormats.FileDrop)).GetValue(0).ToString();
FileInfo fi = new FileInfo(Path);
String str = fi.Extension.ToUpper();
if (str == ".TXT")
{
RichTextBox rt = sender as RichTextBox;
int vv = rt.SelectionStart;
rt.Text = rt.Text.Insert(vv, File.ReadAllText(fi.FullName));
rt.SelectionStart = vv;
}
}
void richTextBox1_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop))
{
e.Effect = DragDropEffects.Copy;
}
else
e.Effect = DragDropEffects.None;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询