This is a migrated thread and some comments may be shown as answers.

drag and drop a text on on a Richtextbox

2 Answers 343 Views
RichTextBox (obsolete as of Q3 2014 SP1)
This is a migrated thread and some comments may be shown as answers.
Justin
Top achievements
Rank 1
Justin asked on 05 Jun 2014, 07:12 AM
Hi there, 

I'm new to telerik controls, I want to drag and drop at text on the richtexbox. Can someone help me?

thinks.

2 Answers, 1 is accepted

Sort by
0
Accepted
Stefan
Telerik team
answered on 06 Jun 2014, 07:33 AM
Hi Justin,

Thank you for writing.

Here is how you can achieve drag and drop from a text box to RadRichTextBox:
    public partial class Form1 : Form
    {
        RadTextBox radTextBox = new RadTextBox();
        Point clickedPoint;
        bool mouseDown = false;
 
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
 
            InitializeComponent();
 
            this.Controls.Add(radTextBox);
            radTextBox.Dock = DockStyle.Top;
            radTextBox.TextBoxElement.TextBoxItem.MouseDown += tb_MouseDown;
            radTextBox.TextBoxElement.TextBoxItem.MouseMove += tb_MouseMove;
 
            AddRichTextBox();
            radRichTextBox1.AllowDrop = true;
            radRichTextBox1.DragOver += radRichTextBox1_DragOver;
            radRichTextBox1.DragDrop += radRichTextBox1_DragDrop;
        }
 
        private static bool IsRealDrag(Point mousePosition, Point initialMousePosition)
        {
            return (Math.Abs(mousePosition.X - initialMousePosition.X) >= SystemInformation.DragSize.Width) ||
                (Math.Abs(mousePosition.Y - initialMousePosition.Y) >= SystemInformation.DragSize.Height);
        }
 
        void tb_MouseMove(object sender, MouseEventArgs e)
        {
            if (mouseDown && IsRealDrag(e.Location, clickedPoint))
            {
                radTextBox.DoDragDrop(radTextBox.Text, DragDropEffects.Copy | DragDropEffects.Move);
                mouseDown = false;
            }
        }
 
        void tb_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == System.Windows.Forms.MouseButtons.Left)
            {
                mouseDown = true;
                clickedPoint = e.Location;
            }
        }
 
        void radRichTextBox1_DragDrop(object sender, DragEventArgs e)
        {
            string text = e.Data.GetData(typeof(string)) as string;
            radRichTextBox1.RichTextBoxElement.Document.CaretPosition.SetPosition(radRichTextBox1.PointToClient(MousePosition));
            radRichTextBox1.Insert(text);
        }
 
        void radRichTextBox1_DragOver(object sender, DragEventArgs e)
        {
            e.Effect = DragDropEffects.Move;
        }
}

I hope that you find this information useful.

Regards,
Stefan
Telerik
 
Check out Telerik Analytics, the service which allows developers to discover app usage patterns, analyze user data, log exceptions, solve problems and profile application performance at run time. Watch the videos and start improving your app based on facts, not hunches.
 
0
Justin
Top achievements
Rank 1
answered on 06 Jun 2014, 08:48 AM
Hi Stefan,

Thank you so much Stefan, it works.

I really appreciate

your help.
Tags
RichTextBox (obsolete as of Q3 2014 SP1)
Asked by
Justin
Top achievements
Rank 1
Answers by
Stefan
Telerik team
Justin
Top achievements
Rank 1
Share this question
or