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のDragEnterイベントとDragDropイベントをバインドします

 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 に答える