Hi,
I am building a 'shape editor' which looks very similar to the Visual Studio form designer. I have a Toolbox control that holds a few 'Shapes' (such as RectangleShape, EllipseShape, TriangleShape, etc) which the user should be able to drag to the design view ("Canvas") in any MDI form. The Toolbox control is just a TreeView except that I add ToolboxItems to it (which are inherited TreeNodes).
This dragging however does not seem to work...
The ToolboxItem (the nodes in the TreeView Toolbox) have a property ShapeType (Type) which holds the Type of the shape they represent.
When adding ToolboxItems I set this type:vb.net Code:
Public Class ToolboxItem Inherits TreeNode Public Property ShapeType As Type End Class
vb.net Code:
toolbox.AddItem(New ToolboxItem("Rectangle", GetType(RectangleShape)) toolbox.AddItem(New ToolboxItem("Ellipse", GetType(EllipseShape)) toolbox.AddItem(New ToolboxItem("Triangle", GetType(TriangleShape))
Now, when I initiate a DragDrop operation I pass this ShapeType as the data:
The 'dragRectangle' check is to ensure that the mouse has moved enough to initiate a drag drop operation (SystemInformation.DragSize).vb.net Code:
Protected Overrides Sub OnMouseMove(ByVal e As MouseEventArgs) If e.Button = System.Windows.Forms.MouseButtons.Left Then If dragRectangle <> Rectangle.Empty AndAlso Not dragRectangle.Contains(e.Location) Then Dim selItem = TryCast(Me.GetNodeAt(e.Location), ToolboxItem) If selItem IsNot Nothing AndAlso selItem.Level = 1 Then Me.DoDragDrop(selItem.ShapeType, DragDropEffects.Move) End If End If End If MyBase.OnMouseMove(e) End Sub
In the 'document' form that contains the Canvas (where the shapes live), I handle the DragEnter and DragDrop events of the Canvas. I get the data of type System.Type, and if it is not nothing I add a new shape at the current location, using Activator.CreateInstance to create the shape from the type that is dragged onto the canvas:
vb.net Code:
Private Sub canvas_DragDrop(ByVal sender As Object, ByVal e As DragEventArgs) Handles canvas.DragDrop Dim data = e.Data.GetData(GetType(System.Type)) If data IsNot Nothing Then Dim pt = Me.PointToClient(New Point(e.X, e.Y)) Dim shape = DirectCast(Activator.CreateInstance(DirectCast(data, Type), pt), Shape) canvas.Shapes.Add(shape) End If End Sub Private Sub canvas_DragEnter(ByVal sender As Object, ByVal e As DragEventArgs) Dim data = e.Data.GetData(GetType(System.Type)) If data IsNot Nothing Then e.Effect = DragDropEffects.Move Else e.Effect = DragDropEffects.None End If End Sub
However... It doesn't work. When I drag a shape from the toolbox onto the canvas the mouse turns into the 'cannot drag' icon (well I've no idea what it's called) and when I release the mouse nothing happens. By debugging I can see that 'data' is Nothing...
I must be doing something wrong but I cannot figure it out. I am clearly passing a Type to the 'data' parameter of the DoDragDrop method... I think I'm tripping over the GetType(Type)... That makes little sense to me, can you even do that?
If not, how else should I handle this?
Thanks!




Reply With Quote