2016-04-14 13 views
0

ドラッグを使用してRichTextBoxにファイルをドロップするとき&ドロップより多くのファイルがドラッグされてもドロップが1つだけ挿入されます。どのように行動を変えることができますか?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)); 
      } 
     } 
    } 
} 

答えて

0

これがデフォルトのコピー動作のようです。 [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 
     } 
    } 
} 
関連する問題