C# RichTextBox仅在拖动时插入第一个文件(&;滴

C# RichTextBox仅在拖动时插入第一个文件(&;滴,c#,winforms,drag-and-drop,richtextbox,C#,Winforms,Drag And Drop,Richtextbox,使用拖放将文件拖放到RichTextBox时,即使拖动了更多文件,也只插入一个文件。我怎样才能改变这种行为 示例表单,说明了问题: using System.Collections.Specialized; using System.Windows.Forms; namespace WindowsFormsApplication3 { public partial class Form1 : Form { RichTextBox rtb; p

使用拖放将文件拖放到RichTextBox时,即使拖动了更多文件,也只插入一个文件。我怎样才能改变这种行为


示例表单,说明了问题:

using System.Collections.Specialized;
using System.Windows.Forms;

namespace WindowsFormsApplication3
{
    public partial class Form1 : Form
    {
        RichTextBox rtb;

        public Form1()
        {
            rtb = new System.Windows.Forms.RichTextBox();
            rtb.Dock = DockStyle.Fill;
            rtb.AllowDrop = true;
            Controls.Add(rtb);

            rtb.DragEnter += Rtb_DragEnter;
            rtb.DragDrop += Rtb_DragDrop;
        }

        private void Rtb_DragEnter(object sender, DragEventArgs e)
        {
            e.Effect = DragDropEffects.Copy;
        }

        private void Rtb_DragDrop(object sender, DragEventArgs e)
        {
            StringCollection sFiles = new StringCollection();
            if (e.Data.GetDataPresent("FileDrop"))
            {
                sFiles.AddRange((string[])e.Data.GetData("FileDrop"));  //returns a list of files
                Clipboard.Clear();
                Clipboard.SetFileDropList(sFiles);
                rtb.Paste(DataFormats.GetFormat(DataFormats.FileDrop));
            }
        }
    }
}

这似乎是默认的复制行为。使用[Ctrl]+[v]时也会出现同样的问题

您可以通过粘贴一个又一个文件来解决此问题:

private void Rtb_DragDrop(object sender, DragEventArgs e)
{
    StringCollection sFiles = new StringCollection();
    if (e.Data.GetDataPresent("FileDrop"))
    {
        foreach (string file in (string[])e.Data.GetData("FileDrop"))
        {
            Clipboard.Clear();
            sFiles.Clear();
            sFiles.Add(file);
            Clipboard.SetFileDropList(sFiles);
            rtb.Paste();  //it's not necessary to specify the format
        }
    }
}