New to Telerik UI for WinForms? Download free 30-day trial

OLE Drag and Drop

RadDiagram is capable of performing drag and drop operation using the native OLE drag and drop support. The example below handles a scenario in which shape elments are being dragged from one diagram control and dropped onto another.

Figure 1: Drag and Drop Between Two Diagrams

WinForms RadDiagram Drag and Drop Between Two Diagrams

In order to achieve the desired result the MouseDown, MouseMove and MouseUp events need to be handled in the first diagram. The drag operation will be started in the handler of the MouseMove event. The second diagram instance needs to be subscribed to the DragEnter and DragDrop events.

Subscribe to Events

public OleDragAndDropForm()
{
    InitializeComponent();
    this.radDiagram2.AllowDrop = true;
    this.radDiagram1.IsDraggingEnabled = false;
    this.radDiagram1.MouseMove += radDiagram1_MouseMove;
    this.radDiagram1.MouseDown += radDiagram1_MouseDown;
    this.radDiagram1.MouseUp += radDiagram1_MouseUp;
    this.radDiagram2.DragEnter += radDiagram2_DragEnter;
    this.radDiagram2.DragDrop += radDiagram2_DragDrop;
}

For the purpose of the example we will define a grid model object storing information about the shapes.

Handling Events

RadDiagram will accept the dragged data only if it is dropped on the diagram element. The PreviewDragDrop event handler will be responsible for reading the data and transforming it to a shape.

Drag and Drop Events

private void radDiagram2_DragDrop(object sender, DragEventArgs e)
{
    Telerik.WinControls.UI.RadDiagram diagram = sender as Telerik.WinControls.UI.RadDiagram;
    Point point = diagram.PointToClient(new Point(e.X, e.Y));
    RadDiagramShape draggedItem = e.Data.GetData(typeof(RadDiagramShape)) as RadDiagramShape;
    this.radDiagram1.RemoveShape(draggedItem);
    draggedItem.Position = point;
    diagram.AddShape(draggedItem);
    this.mouseDownPosition = Point.Empty;
}
private void radDiagram2_DragEnter(object sender, DragEventArgs e)
{
    e.Effect = DragDropEffects.Copy;
}
private Point mouseDownPosition;
private void radDiagram1_MouseDown(object sender, MouseEventArgs e)
{
    this.mouseDownPosition = e.Location;
}
private void radDiagram1_MouseUp(object sender, MouseEventArgs e)
{
    this.mouseDownPosition = Point.Empty;
}
private void radDiagram1_MouseMove(object sender, MouseEventArgs e)
{
    if (!(e.Button == MouseButtons.Left))
    {
        return;
    }
    if (this.ShouldBeginDrag(this.mouseDownPosition, e.Location))
    {
        RadDiagramShape draggedItem = this.radDiagram1.ElementTree.GetElementAtPoint(this.mouseDownPosition).Parent as RadDiagramShape;
        if (draggedItem != null)
        {
            ((Telerik.WinControls.UI.RadDiagram)sender).DoDragDrop(draggedItem, DragDropEffects.Copy);
        }
    }
}
private bool ShouldBeginDrag(Point current, Point capture)
{
    Size dragSize = SystemInformation.DragSize;
    Rectangle dragRect = new Rectangle(capture.X - dragSize.Width / 2,
        capture.Y - dragSize.Height / 2, dragSize.Width, dragSize.Height);
    return !dragRect.Contains(current);
}