【问题标题】:Dragging files into rich textbox to read text in file将文件拖入富文本框以读取文件中的文本
【发布时间】:2013-03-11 07:39:38
【问题描述】:

我在将文件拖放到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);
                }
            }
        }
    }

关于如何完成这项工作的任何想法?

【问题讨论】:

  • 设置属性 EnableAutoDragDrop = False;因为您在 RicHTextBox 中获得了图标,请按照我的回答中的事件处理程序。它会工作
  • 抱歉,将这些行添加到 Designer.cs 中,它可以工作,谢谢
  • 请作为答案

标签: c# .net drag-and-drop text-files richtextbox


【解决方案1】:

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

 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);
                }

            }
        }

【讨论】:

  • 我收到了三个错误。这行代码中有两个if (e.KeyStates == DragDropKeyStates.ShiftKey),一个在KeyStates 下,一个在DragDropKeyStates 下。并在Documents下使用此代码System.Windows.Documents.TextRange range;
  • using System.Windows.Forms;是的
  • 给我 5 分钟。将更新我的 WinForms 代码。此代码属于 WPF。
  • 哇,我很惊讶!您的方法打开大型文本文件的速度比记事本快 3 倍!为这个方法 +1
【解决方案2】:

使用它为 Designer.cs

中的 RichTextBox 绑定 DragEnterDragDrop 事件
 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;

            }

【讨论】:

  • 没有“AllowDrop”。这是一个富文本框。最接近的是“EnableAutoDragDrop”,它已经设置为 true
  • 您正在使用 txtFileContent 进行拖放,AllowDrop 是 TextBox 可用的属性。请检查 TextBox 的属性。你的代码工作正常。只需要为 DragEnter 编写事件并为 AllowDrop txtFileContent 设置属性 True
  • 没有“AllowDrop”,因为我说这是一个富文本框。是的代码正在工作,但对于常规文本框。我对其进行了修改,因为我需要它与richTextBox 一起使用,而它没有
  • 那么你说的这个事件是什么意思 txtFileContent_DragDrop.
  • 请检查一下,我想这就是你的意思,social.msdn.microsoft.com/Forums/en-AU/winforms/thread/…
猜你喜欢
  • 2012-03-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多