Richtextbox的C#Dragdrop事件未触发

时间:2011-10-13 20:35:40

标签: c# drag-and-drop richtextbox

我正在构建一个只带有两个富文本框的面板的简单表单,唯一的功能是允许用户在两​​个富文本框之间拖放文本。

以下是代码:

using System;
using System.Windows.Forms;

namespace Exmaple
{
    class PanelForm : Form
    {
        private RichTextBox rtb1, rtb2;

        public PanelForm()
        {
            this.rtb1 = new RichTextBox();
            this.rtb2 = new RichTextBox();

            this.rtb1.AllowDrop = true;
            this.rtb2.AllowDrop = true;
            //this.rtb1.DragEnter += new DragEventHandler(this.rtb_DragEnter);
            //this.rtb1.DragDrop += new DragEventHandler(this.rtb1_DragDrop);
            this.rtb2.DragEnter += new DragEventHandler(this.rtb_DragEnter);
            this.rtb2.DragDrop += new DragEventHandler(this.rtb2_DragDrop);
            this.rtb2.DragOver += new DragEventHandler(this.rtb_DragOver);

            Panel panel = new Panel();

            panel.Controls.Add(rtb1);
            panel.Controls.Add(rtb2);
            this.Controls.Add(panel);

            this.rtb1.Dock = DockStyle.Left;
            this.rtb2.Dock = DockStyle.Right;
            panel.Dock = DockStyle.Fill;

            this.Text = "Panel Form";
            this.Width = 600;
            this.Height = 300;
        }

        private void rtb_DragEnter(object sender, DragEventArgs args)
        {
            if (args.Data.GetDataPresent(DataFormats.Rtf))
            {
                args.Effect = DragDropEffects.Copy;
            }
            else
            {
                args.Effect = DragDropEffects.None;
            }
        }

        private void rtb_DragOver(object sender, DragEventArgs args)
        {
            if (args.Data.GetDataPresent(DataFormats.Rtf))
            {
                args.Effect = DragDropEffects.Copy;
            }
            else
            {
                args.Effect = DragDropEffects.None;
            }
        }

        private void rtb1_DragDrop(object sender, DragEventArgs args)
        {
            this.rtb1.SelectedRtf = args.Data.GetData(DataFormats.Rtf).ToString();
        }

        private void rtb2_DragDrop(object sender, DragEventArgs args)
        {
            this.rtb2.SelectedRtf = args.Data.GetData(DataFormats.Rtf).ToString();
        }
    }
}

问题是,触发了rtb_DragEnter事件,但是没有触发rtb2_DragDrop事件。 allowDrop属性设置为true,并且在rtb_DragEnter事件中,DragDropEffects设置为Copy。还有一件事是,光标是一个负号,为什么呢?

平台是win7,我使用的是VS2010和.NET版本4。

任何有任何意见或建议的人都欢迎!

0 个答案:

没有答案