DragDrop data

rbulph

Junior Contributor
Joined
Feb 17, 2003
Messages
397
I'd like to send a reference to a type with a dragdrop routine, but can't get it to work. Any thoughts? Here's the code I'm using:

Code:
Public Class Form1

    Dim s As Type

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.PictureBox1.AllowDrop = True

    End Sub

    Private Sub PictureBox1_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles PictureBox1.DragDrop

        Dim f As Type
        f = e.Data.GetData(GetType(Type))
        Debug.Print(f Is Nothing)   'returns True. How do I extract the Type from e.Data?

    End Sub

    Private Sub PictureBox1_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles PictureBox1.DragEnter
        e.Effect = DragDropEffects.Copy
    End Sub

    Private Sub Button2_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Button2.MouseMove

        If e.Button = Windows.Forms.MouseButtons.Left Then
            s = GetType(class1)

            Button1.DoDragDrop(s, DragDropEffects.Copy)
        End If
    End Sub

End Class

Public Class class1
    Dim x As Long
End Class
 
As simple as the idea of getting the type of a dragged object may seem, I really don't think it will be. By using e.Data.GetFormats() you can get a list of the objects names which in theory allows you to use Type.GetType() to get a type object. The difficulty however is that if the object is not in the current assembly you need to specify the AssemblyQualifiedName, which isn't easy to find unless you have an object of that type in the first place.

You could create a method that looped through the assemblies looking but this would be extremely in-efficient. Your best bet will problably be a work around, but how exactly this will be achieved is dependent on what exactly your tying to achieve. If for example all the objects you will be dragging are from System.Windows.Forms then you can quite easily generate the AssemblyQualifiedName.
 
No, actually that's not what I'm trying to do. I want the data to be a Type object, not a control that's being dragged. On experimenting further I've found that you can do what I want with code like:
Code:
Dim t As Type = e.Data.GetData(e.Data.GetFormats(True)(0))
to extract the Type that you sent in the DoDragDrop method. The format that the data is in is a WindowsForms10PersistentObject which I can't find in the object browser, and that's why I was having problems, but if you write the code in one line, and don't ever try to declare a WindowsForms10PersistentObject, then it seems OK.
 
Back
Top