Drag and Drop Form Object

false74

Well-known member
Joined
Aug 4, 2010
Messages
76
Programming Experience
Beginner
What I want to do is have the ability to drag a form object into a panel to add that form into the panels controls. What I have is a base class for my form called "DForm" and any other type of form that will have this drag/drop ability will be a child of it. I have another form that has a panel inside which accepts the drag/drop event and adds that form into its controls (a form inside a panel). My issue is that when I try to pass the DForm object when calling the DoDragDrop() function it is of the child DForm object and not of DForm. So I cannot do the drop unless I specify the specific child type and not just the base/parent type. Below is a shortened version better explaining my issue.

Base Class:
VB.NET:
Public Class DForm
    Inherits Form

    Private Sub dragform() Handles Me.MouseDown
        If TopLevel Then
            DoDragDrop(Me, DragDropEffects.Move)
        End If
    End Sub
End Class

Child form:
VB.NET:
Public Class SomeChildForm
    Inherits DForm

    'other code not shown
End Class

Main form that allows a drop: The issue is in this form when dragdropping
VB.NET:
Public Class MasterForm
    Inherits Form
    Private WithEvents formcontainer As New Panel With {.Dock = DockStyle.Fill, .AllowDrop = True}
    Public Sub New()
        Me.Controls.Add(formcontainer)
    End Sub
    Private Sub containerdragenter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles formcontainer.DragEnter
        If (e.Data.GetDataPresent(GetType(DForm))) Then
            e.Effect = DragDropEffects.Move
        End If
    End Sub
    Private Sub containerdragdrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles formcontainer.DragDrop
        If e.Data.GetDataPresent(GetType(DForm)) Then 'WHEN "SomeChildForm" IS DROPPED, IT IS OF TYPE "SomeChildForm" AND NOT DForm. How do I fix this?
            'cast object as DForm then add it into formcontainer.controls
            MsgBox("added")
        End If
    End Sub
End Class

So when I drag a SomeChildForm object into the panel it is of that type (obviously) and not of type DForm. Because I have multiple classes which inherit DForm I need to be able to take any class that is or inherits DForm. How can I 'fix' this issue?
 
If you call GetDataFormats then you will get a list of all the formats the data is available in. I'm not sure what you'll get back but you should be able to use it to determine whether it is a type derived from DForm.
 
ah, good thinking!
VB.NET:
    Private Sub containerdragenter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles formcontainer.DragEnter
        If Type.GetType(e.Data.GetFormats(True)(0)).IsSubclassOf(GetType(DForm)) Then
            e.Effect = DragDropEffects.Move
        End If
    End Sub
    Private Sub containerdragdrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles formcontainer.DragDrop
        If Type.GetType(e.Data.GetFormats(True)(0)).IsSubclassOf(GetType(DForm)) Then
            Dim form As DForm = CType(e.Data.GetData(Type.GetType(e.Data.GetFormats(True)(0))), DForm)
            'do some stuff here
        End If
    End Sub
 
Back
Top