How to implement drag drop from another tool window

I'm trying to implement drag drop from another tool window onto the design surface of my DSL. Once the drop is complete, I want to create various components and shapes on the design surface. This would seem to be a common requirement, but it has been very frustrating and difficult to implement.

My code is rather straight-forward, but the behavior of the related event handler methods in the Diagram class is inconsistent. I need some basic guidance on the correct strategy for implementing this kind of functionality.

One thing I don't understand is why the Diagram.OnDragOver event would not be called depending on whether I reference a custom interface in my override. For example, if I uncomment the following line in my overridden method, the event handler is no longer called.

Code Snippet

public override void OnDragOver(DiagramDragEventArgs e) {

base.OnDragOver(e);

if (e.Data.GetDataPresent("IMyCustomInterface")) {

try {

/* IMyCustomInterface obj = e.Data.GetData("IMyCustomInterface") as IMyCustomInterface; */

e.Effect = DragDropEffects.Copy;

} catch {}

}

}

At this point, any help would be appreciated.

[1459 byte] By [JohnHolliday] at [2008-1-7]
# 1

John,

The code snippet below works with a DSL based on the Class Diagrams template, and allows you drag and drop System.String data onto the design surface. The drop action is to create a new Comment and CommentBoxShape, with the Comment.Text property being set to the System.String value that was dragged. For example, you could drag a file from the Solution Explorer onto the design surface to create a comment that displays the file name.

I can't see any reason why the OnDragOver event handler would not be called if you uncomment the line in your code. Your exception handler might be suppressing an error that occurs on deserialization so the e.Effect = Copy isn't be set, but that wouldn't stop the event handler being called. I tried your code in a test class, and the event handler was called whether or not I had the line commented out.

Regards,

Duncan

Code Snippet

partial class ClassDiagram

{

public override void OnDragOver(DiagramDragEventArgs e)

{

// Assumes the following "using" statements:

// using Microsoft.VisualStudio.Modeling;

// using Microsoft.VisualStudio.Modeling.Diagrams;

// using System.Windows.Forms;

base.OnDragOver(e);

// Don't do anything if the event has already been handled

// (e.g. could mess up dragging from the toolbox)

if (!e.Handled)

{

// We're only interested if there is string data and the

// source of the drag-drop operation allows copy operations.

if (e.Data.GetDataPresent("System.String") &&

(System.Windows.Forms.DragDropEffects.Copy == (e.AllowedEffect & System.Windows.Forms.DragDropEffects.Copy)))

{

e.Effect = System.Windows.Forms.DragDropEffects.Copy;

}

}

}

public override void OnDragDrop(DiagramDragEventArgs e)

{

base.OnDragDrop(e);

if (!e.Handled)

{

if (e.Effect == System.Windows.Forms.DragDropEffects.Copy &&

e.Data.GetDataPresent("System.String"))

{

using (Transaction t = this.Store.TransactionManager.BeginTransaction("Add comment"))

{

// Create the new comment and link it to the model

Comment newComment = new Comment(this.Store);

newComment.Text = e.Data.GetData("System.String") as string;

((ModelRoot)this.ModelElement).Comments.Add(newComment);

// Explicitly create a new comment shape, link it to the

// new comment, and position it.

// If we don't do this, a new shape will be created for us

// somewhere on the diagram.

CommentBoxShape newShape = new CommentBoxShape(this.Store);

newShape.ModelElement = newComment;

newShape.AbsoluteBounds = new RectangleD(e.MousePosition.X, e.MousePosition.Y, 1.2, 0.8);

this.NestedChildShapes.Add(newShape); // link the shape to the diagram

t.Commit();

}

}

}

}

}

DuncanP-MSFT at 2007-10-2 > top of Msdn Tech,Visual Studio,Visual Studio Extensibility...
# 2

Duncan,

Thanks for the detailed response. It worked as promised, but only after uninstalling the DXCore package from DevExpress. Seems there was some low-level interference with the DSL tools. Not sure exactly what it was - might be worth pursuing, but I've spent WAY too long just getting this drag-drop scenario implemented.

Thanks also for the tip on explicitly placing the new shape onto the diagram. This helps a lot and makes the user experience more intuitive.

John

JohnHolliday at 2007-10-2 > top of Msdn Tech,Visual Studio,Visual Studio Extensibility...

Visual Studio

Site Classified