New to Telerik UI for WinFormsStart a free 30-day trial

Drag and Drop Text with Cursor Movement in RadSyntaxEditor for WinForms

Updated over 6 months ago

Environment

Product VersionProductAuthor
2023.3.1114RadTreeView for WinFormsDinko Krastev

Description

In the following tutorial, we will demonstrate how to drag-drop text from an external source while moving the cursor to the desired drop position.

syntaxeditor-dragdrop-cursor-position 001

Solution

To achieve this, you can follow the steps below:

  1. Set the AllowDrop property of the RadSyntaxEditor control to true.
  2. Handle the DragOver event of the RadSyntaxEditor control and set the Effect property of the DragEventArgs to DragDropEffects.All. This allows the control to accept the dragged text.
  3. In the DragOver event handler, move the cursor to the desired drop position by focusing the RadSyntaxEditor control and using the MoveToPosition() method.
  4. Handle the DragDrop event of the RadSyntaxEditor control and insert the dropped text at the current cursor position using the Insert() method of the SyntaxEditorElement.Document property.

Here is an example implementation:

C#

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        this.radSyntaxEditor1.AllowDrop = true;
        this.radSyntaxEditor1.DragOver += radSyntaxEditor1_DragOver;
        this.radSyntaxEditor1.DragDrop += radSyntaxEditor1_DragDrop;
    }

    private void radSyntaxEditor1_DragDrop(object sender, DragEventArgs e)
    {
        string txt = e.Data.GetData(DataFormats.Text).ToString();
        this.radSyntaxEditor1.SyntaxEditorElement.Document.Insert(GetCaretPostion().Index, txt);
    }

    private void radSyntaxEditor1_DragOver(object sender, DragEventArgs e)
    {
        e.Effect = DragDropEffects.All;
        this.radSyntaxEditor1.SyntaxEditorElement.CaretPosition.MoveToPosition(GetCaretPostion());
        this.radSyntaxEditor1.Focus();
    }

    private CaretPosition GetCaretPostion()
    {
        Point pt = this.radSyntaxEditor1.SyntaxEditorElement.PointFromScreen(MousePosition);
        CaretPosition caretPosition = this.radSyntaxEditor1.SyntaxEditorElement.GetPositionFromControlPoint(pt);
        return caretPosition;
    }
}

Make sure to add empty space in the control before dragging and dropping the text to have room for the drop operation.

In this article
EnvironmentDescriptionSolution
Not finding the help you need?
Contact Support