Runtime DesignSurface and Drag 'n' Drop

a_jam_sandwich

Junior Contributor
Joined
Dec 10, 2002
Messages
367
Location
Uk
Hi,


I’m currently creating a control inherited from the System.ComponentModel.Design.DesignSurface. So far I have been able to add & remove controls to and from the surface. Using the Selection service to find out selected controls on the surface but what I need to do now is have the ability to drag controls on this service from a very limited list of usercontrols contained within a Toolstrip and place them on the surface.

The controls will need to have some properties set one places on the surface for example there is a field list that once you drag the field onto the surface the field properties are already set within that control based off what as field was initaly dragged.

The only thing I need to do is figure out how to actually initate the drag and drop on a designer surface, searching the .NET had not thown up much in the was of help.

Creation code for the control snippet.

Code:
Public Class Designer
    Inherits DesignSurface

    Private mobjDragDrop As DesignerDragDrop
    Private mobjHost As Control = Nothing
    Private mobjSelectionService As ISelectionService = Nothing
    Private WithEvents mobjDesignerHost As IDesignerHost = Nothing

    Private Function New(ByVal RootType As Type) As IComponent
        ' Get the designer host/services
        Me.mobjDesignerHost = GetServiceDesignerHost()
        Me.mobjSelectionService = GetServiceSelectionService()

        ' Exit if the designer already exists
        If (mobjDesignerHost.RootComponent IsNot Nothing) Then Return mobjDesignerHost.RootComponent

        ' Root componant
        Call Me.BeginLoad(RootType)

        ' Get the designer host
        mobjDesignerHost = GetServiceDesignerHost()

        ' Root control
        Dim ctlControl As IComponent = Nothing
        ctlControl = mobjDesignerHost.RootComponent

        Return ctlControl
    End Function

Any pointer would be greatly appreciated

Thanks

Andy
 
I've created a control that implements IToolboxService and then added this control as a service to the DesignSurface, this now allow me to drag controls from my toolbox onto the designer form.

Only problem now is how to set properties for the control such as the name and or text etc?


Thanks

Andy
 
So much fun ... NOT

Right I can drop controls onto my form using

Code:
        Dim objToolboxItem As New ToolboxItem(GetType(ExtendedTextbox))

        Dim objToolboxService As IToolboxService = Me
        Dim objDataObject As DataObject = objToolboxService.SerializeToolboxItem(objToolboxItem)

        Call Me.DoDragDrop(objDataObject, DragDropEffects.Copy)

This works fine for droping a textbox control that i've inherited from. The problem comes when I create inherited ToolboxItem which i've i'm using (hope to) to set default properties. The code i'm using below will not drag and drop on the form!

Code:
<Serializable(), PermissionSetAttribute(SecurityAction.Demand, Name:="FullTrust")> _
Public Class ToolboxItemFieldBox
    Inherits System.Drawing.Design.ToolboxItem

    Public Sub New()
        MyBase.New()
    End Sub

    Public Sub New(ByVal ToolType As Type)
        MyBase.New(GetType(ExtendedTextbox))
    End Sub

End Class

I'm at my wits end creating a designsurface to host a form editor, this has been the baine of my life this last week.

Many thanks

Andy
 
In the end it was because I attempted to cast incorrectly the type on the deserialization of the toolbox item in the toolbox control.

When adding a toolbox item on the designsurface the toolbox item gets serialized when calling DataObject and adding it to DoDragDrop, as you drag the item onto the designsurface this item it attempted to be deserialized in the toolbox, if it can the item is then availble for drag drop if not the deserialization returns nothing.

Hope this helps anyone if they have the same problems

Andy
 
Back
Top