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

How to DragDrop from MS TreeView to RadSyntaxEditor

Updated over 6 months ago

Environment

Product VersionProductAuthor
2023.1.314RadSyntaxEditor for WinFormsDinko Krastev

Description

In this example, we will demonstrate how to drag-drop native TreeView nodes and insert text in RadSyntaxEditor on the drop mouse location.

syntaxeditor-dragdrop-ms-treeview001

Solution

To make this work, first, we need to allow the drop by setting the AllowDrop property to true. Then we can subscribe to the OLE Drag-Drop events: ItemDrag of the TreeView, DragOver and DragDrop of the RadSyntaxEditor. In the ItemDrag and DragOver, we need to allow the drop by using DragDropEffects.All enumeration. So what's left is to get the drag node text and insert it at the current mouse location over the RadSyntaxEditor document.

C#
public RadForm1()
{
    InitializeComponent();
	this.radSyntaxEditor1.AllowDrop = true;
	this.treeView1.ItemDrag += new ItemDragEventHandler(treeView1_ItemDrag);
	this.radSyntaxEditor1.DragOver += RadSyntaxEditor1_DragOver;
	this.radSyntaxEditor1.DragDrop += RadSyntaxEditor1_DragDrop;
}

private void treeView1_ItemDrag(object sender, ItemDragEventArgs e)
{
    if (e.Button == MouseButtons.Left)
    {
        DoDragDrop(e.Item, DragDropEffects.All);
    }
}
private void RadSyntaxEditor1_DragOver(object sender, DragEventArgs e)
{
    e.Effect = DragDropEffects.All;
}

private void RadSyntaxEditor1_DragDrop(object sender, DragEventArgs e)
{
    TreeNode draggedNode = (TreeNode)e.Data.GetData(typeof(TreeNode));
    if (draggedNode != null)
    {
        var text = draggedNode.Text;        
		Point pt = this.radSyntaxEditor1.SyntaxEditorElement.PointFromScreen(MousePosition);
		CaretPosition caretPosition = this.radSyntaxEditor1.SyntaxEditorElement.GetPositionFromControlPoint(pt);
		this.radSyntaxEditor1.SyntaxEditorElement.Document.Insert(caretPosition.Index, text);
        
    }
}

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