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

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

4

2 回答 2

4

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

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

            }
        }
于 2013-03-11T07:46:37.757 回答
3

使用它为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;

            }
于 2013-03-11T07:59:11.037 回答