Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / August 2005

Tip: Looking for answers? Try searching our database.

Drag and Drop Question

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Bagger - 12 Aug 2005 09:01 GMT
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!
Jakob Christensen - 12 Aug 2005 15:05 GMT
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.

Signature

http://www.dotninjas.dk
http://www.powerbytes.dk

> 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
[quoted text clipped - 4 lines]
> 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!
Bagger - 13 Aug 2005 20:40 GMT
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

> I suppose you could register new formats with DataFormats.Format.GetFormat.  
> But objects of a serializable type can be dragged and dropped between forms
[quoted text clipped - 14 lines]
> > 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!
Bagger - 13 Aug 2005 21:02 GMT
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?

> 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
[quoted text clipped - 123 lines]
> > > 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!
Jakob Christensen - 15 Aug 2005 08:33 GMT
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.
Signature

http://www.dotninjas.dk
http://www.powerbytes.dk

> 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
[quoted text clipped - 129 lines]
> > > > 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!

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.