2013-03-11 37 views
1

我在将文件拖放到richTextBox时遇到问题,每当我将文本文件拖动到文本文件上时,它就会变成文本文件的图片及其名称在它下面。双击该文件并使用系统默认应用程序(即文本文件的记事本等)打开它。基本上它在richTextBox中创建快捷方式,当我希望它读取文件中的文本时。在此基础上的代码,从文件文本将文件拖动到丰富的文本框中以读取文件中的文本

应该提取到richTextBox1

class DragDropRichTextBox : RichTextBox 
    { 
    public DragDropRichTextBox() 
    { 
     this.AllowDrop = true; 
     this.DragDrop += new DragEventHandler(DragDropRichTextBox_DragDrop); 
    } 

    private void DragDropRichTextBox_DragDrop(object sender, DragEventArgs e) 
    { 
     string[] fileNames = e.Data.GetData(DataFormats.FileDrop) as string[]; 

     if (fileNames != null) 
     { 
      foreach (string name in fileNames) 
      { 
       try 
       { 
        this.AppendText(File.ReadAllText(name) + "\n"); 
       } 
       catch (Exception ex) 
       { 
        MessageBox.Show(ex.Message); 
       } 
      } 
     } 
    } 

如何使这项工作任何想法?

+0

的DragDrop事件RichTextBox的设置属性EnableAutoDragDrop = FALSE;因为你在RicHTextBox中获得了图标,请在我的答案中按照事件处理程序。它将工作 – 2013-03-11 09:25:44

+0

对不起,将行添加到Designer.cs,它的工作原理,谢谢 – Kazankoph 2013-03-11 09:37:11

+0

作为回答请 – 2013-03-11 09:40:10

回答

3

在读入文件之前,您需要检查draged对象。尝试下面的代码。

public partial class Form1 : Form 
    { 
     public Form1() 
     { 
      InitializeComponent(); 
      richTextBox1.DragDrop += new DragEventHandler(richTextBox1_DragDrop); 
      richTextBox1.AllowDrop = true; 
     } 

     void richTextBox1_DragDrop(object sender, DragEventArgs e) 
     { 
      object filename = e.Data.GetData("FileDrop"); 
      if (filename != null) 
      { 
       var list = filename as string[]; 

       if (list != null && !string.IsNullOrWhiteSpace(list[0])) 
       { 
        richTextBox1.Clear(); 
        richTextBox1.LoadFile(list[0], RichTextBoxStreamType.PlainText); 
       } 

      } 
     } 
+0

我收到三个错误。在这行代码中有两个if(e.KeyStates == DragDropKeyStates.ShiftKey)''在'KeyStates'下,另一个在'DragDropKeyStates'下。并使用此代码'System.Windows.Documents.TextRange范围;''文件' – Kazankoph 2013-03-11 07:54:49

+0

下'你使用winforms? – JSJ 2013-03-11 08:28:36

+0

在'使用System.Windows.Forms;'是 – Kazankoph 2013-03-11 08:39:33

2

使用这种结合dragEnter事件Designer.cs

this.richTextBox1.AllowDrop = true; this.richTextBox1.DragDrop += new System.Windows.Forms.DragEventHandler(this.textBox1_DragDrop); this.richTextBox1.DragEnter += new System.Windows.Forms.DragEventHandler(this.textBox1_DragEnter); 

private void textBox1_DragDrop(object sender, DragEventArgs e) 
      { 
       try 
       { 
        Array a = (Array)e.Data.GetData(DataFormats.FileDrop); 
        if (a != null) 
        { 
         string s = a.GetValue(0).ToString(); 
         this.Activate(); 
         OpenFile(s); 
        } 
       } 
       catch (Exception ex) 
       { 
        MessageBox.Show("Error in DragDrop function: " + ex.Message); 
       } 

      } 

      private void OpenFile(string sFile) 
      { 
       try 
       { 
        StreamReader StreamReader1 = new StreamReader(sFile); 
        richTextBox1.Text = StreamReader1.ReadToEnd(); 
        StreamReader1.Close(); 
       } 
       catch (Exception ex) 
       { 
        MessageBox.Show(this, ex.Message, "Error loading from file"); 
       } 

      } 

      private void textBox1_DragEnter(object sender, DragEventArgs e) 
      { 
       if (e.Data.GetDataPresent(DataFormats.FileDrop)) 
        e.Effect = DragDropEffects.Copy; 
       else 
        e.Effect = DragDropEffects.None; 

      } 
+0

没有“AllowDrop”。这是一个丰富的文本框。最近的是“EnableAutoDragDrop”,它已被设置为true – Kazankoph 2013-03-11 08:02:18

+0

您正在使用txtFileContent拖放,AllowDrop属性可供TextBox使用。请检查TextBox的属性。你的代码工作正常。唯一需要为DragEnter写入事件并为AllowDrop设置True属性txtFileContent – 2013-03-11 08:17:18

+0

没有“AllowDrop”,因为我说这是一个富文本框。是代码正在工作,但用于常规文本框。我修改它,因为我需要它与richTextBox,它不 – Kazankoph 2013-03-11 08:24:50