drag and drop a text on on a Richtextbox

3 posts, 1 answers
  1. Justin
    Justin avatar
    2 posts
    Member since:
    Jun 2014

    Posted 05 Jun 2014 Link to this post

    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. Answer
    Stefan
    Admin
    Stefan avatar
    2890 posts

    Posted 06 Jun 2014 Link to this post

    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.
     
  3. UI for WinForms is Visual Studio 2017 Ready
  4. Justin
    Justin avatar
    2 posts
    Member since:
    Jun 2014

    Posted 06 Jun 2014 in reply to Stefan Link to this post

    Hi Stefan,

    Thank you so much Stefan, it works.

    I really appreciate

    your help.
Back to Top