开发者

C# Dragdrop event of Richtextbox is not firing

I am building a simple form with only a panel with two rich text boxes, and the only functionality is to allow the user to drag and drop text between the two rich text boxes.

Here is the code:

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.All开发者_JS百科owDrop = 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();
        }
    }
}

The problem is, the rtb_DragEnter event is fired, but the rtb2_DragDrop is not. The allowDrop property is set to true, and in the rtb_DragEnter event the DragDropEffects is set to Copy. One more thing is, the cursor is a negative sign, why is that?

The plateform is win7, and I am using VS2010 with .NET version 4.

Anyone having any comment or suggestion is welcome!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜