Drag Drop a System.Type from a TreeView
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.
vb.net Code:
Public Class ToolboxItem
Inherits TreeNode
Public Property ShapeType As Type
End Class
When adding ToolboxItems I set this type:
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:
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
The 'dragRectangle' check is to ensure that the mouse has moved enough to initiate a drag drop operation (SystemInformation.DragSize).
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!
Re: Drag Drop a System.Type from a TreeView
In your DragEnter event handler, try calling e.Data.GetDataFormats to see if you can see what format(s) the data is available in.
Re: Drag Drop a System.Type from a TreeView
Hm, nevermind, I decided to just pass the entire ToolboxItem. That seems to work as I expected. When I have some time I'll try it with the Type again cause I still want to know why that isn't working.