Drag and Drop Question

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I've been looking around for info on this, but not finding much. What I'm
trying to do is allow a user to drag and drop objects between two forms in my
app. The objects are classes that I've built and I want the target form to
be able to recognize what kind of type is being dragged over it and dropped.
From what I could tell from the MSDN docs, I apparently need to add a custom
dataformat type to the existing list, and/or to register a new data type in
the registry. Can anyone explain exactly how I need to do this, or point me
to a tutorial somewhere? I'm pretty stuck on this one. Thanks!
 
I suppose you could register new formats with DataFormats.Format.GetFormat.
But objects of a serializable type can be dragged and dropped between forms
without any problems. I have created a small sample for you:
http://www.dotninjas.dk/code/form1.cs.

If you start two instances of the appplication, the code allows you to drag
an object of a serializable type between the forms of the two applications.

HTH, Jakob.
 
Ok, I tried it the way you said, but still can't get it working. I'm doing
this in VB.Net btw. I ran the code you sent and it worked fine, but when I
tried to get mine working the same way, I'm still having problems. Mainly,
the target form isn't recognizing the object that is being dragged. I'm not
sure exactly what I'm doing wrong. I'm posting the relevant parts of my
code. Think you can help?

Private Sub lbT1Systems_MouseDown(ByVal sender As Object, ByVal e As
System.Windows.Forms.MouseEventArgs) Handles lbT1Systems.MouseDown
If e.Button = MouseButtons.Left Then
If lbT1Systems.SelectedIndex > -1 Then
Dim obj As ManagedObject =
lbT1Systems.Items(lbT1Systems.SelectedIndex)
Debug.WriteLine("Dragging object: " & obj.Name)
lbT1Systems.DoDragDrop(obj, DragDropEffects.Copy)
End If
End If
End Sub

Private Sub ViewsForm_DragEnter(ByVal sender As Object, ByVal e As
System.Windows.Forms.DragEventArgs) Handles MyBase.DragEnter
If (e.Data.GetDataPresent(GetType(ManagedObject))) Then
e.Effect = DragDropEffects.Copy
Else
e.Effect = DragDropEffects.None
End If
End Sub

Private Sub ViewsForm_DragDrop(ByVal sender As Object, ByVal e As
System.Windows.Forms.DragEventArgs) Handles MyBase.DragDrop
Dim obj As ManagedObject =
CType(e.Data.GetData(GetType(ManagedObject)), ManagedObject)
Debug.WriteLine("Object dropped: " & obj.Name)
colMObj.Add(obj)
End Sub

<Serializable()> _
Public Class ManagedObject
Inherits Object
Protected _Name As String
Protected _ManagedType As String
Protected _Info As String
Protected _Location As Point
Protected _Size As Size
Protected _Relationships As New RelationCollection

Public Property Name()
Get
Return _Name
End Get
Set(ByVal Value)
_Name = Value
SetInfo()
End Set
End Property

Public Property ManagedType() As String
Get
Return _ManagedType
End Get
Set(ByVal Value As String)
_ManagedType = Value
SetInfo()
End Set
End Property

Public ReadOnly Property Info() As String
Get
Return _Info
End Get
End Property

Public Property Relationships() As RelationCollection
Get
Return _Relationships
End Get
Set(ByVal Value As RelationCollection)
_Relationships = Value
End Set
End Property

Public Property Location() As Point
Get
Return _Location
End Get
Set(ByVal Value As Point)
_Location = Value
End Set
End Property

Public Property Size() As Size
Get
Return _Size
End Get
Set(ByVal Value As Size)
_Size = Value
End Set
End Property

Protected Overridable Sub SetInfo()
_Info = _Name
End Sub

Public Overrides Function ToString() As String
Return _Name
End Function

End Class
 
Oops. I found my mistake. I was actually passing an object that was derived
from the ManagedObject class, which is why it wasn't being identified
properly. Now I have a new problem, which is that I want to be able to pass
these various kinds of derived objects and have the target form accept any
object that is derived from ManagedObject. Is that possible?
 
I think your target form will have to have a list of all the possible types
being dragged. The form can then query for all the known types to find the
type of the object being dragged by using GetDataPresent in the DragDrop
event handler. When GetDataPresent returns true you can convert the type of
the dragged object by using Convert.ChangeType.

HTH, Jakob.
 
Back
Top