ARTICLE

Drag and Drop Revisited

Posted by Mike Gold Articles | Visual Basic 2010 September 05, 2003
I've had a couple inquiries on how to do drag and drop in .NET again, so I've put together a simple app for dragging text from a TreeView to a TreeView and a TreeView to a ListBox.
Download Files:
 
Reader Level:



Description.

I've had a couple inquiries on how to do drag and drop in .NET again, so I've put together a simple app for dragging text from a TreeView to a TreeView and a TreeView to a ListBox.

Basically there are three events you need to handle when dragging from one item to another. In the case of TreeViews and ListViews, you need to handle the ItemDrag, DragEnter, and DragDrop events. In the case of other components such as listboxes ItemDrag is replaced by the MouseDown event. Also, all drop targets must have the AllowDrop property set to true. In the ItemDrag event you call the DoDragDrop routine as shown below:

Listing 1.

Private Sub treeView1_ItemDrag(sender As Object, e As System.Windows.Forms.ItemDragEventArgs)
Dim strItem As String = e.Item.ToString()
' Start the Drag Operation
DoDragDrop(strItem, DragDropEffects.Copy Or DragDropEffects.Move)
End Sub 'treeView1_ItemDrag

In the case above, we are dragging a string, however, you can drag bitmaps and metafiles as well by replacing the first parameter of DoDragDrop.

In order to maintain the "Drag" look, you need to handle the DragEnter event in both the source and the drop destination. The DragEnter event is shown below:

Listing 2.

Private Sub treeView2_DragEnter(sender As Object, e As System.Windows.Forms.DragEventArgs)
' Determine if we are dragging something, if we are, set the effect
If e.Data.GetDataPresent(DataFormats.Text) Then
e.Effect = DragDropEffects.Copy
Else
e.Effect = DragDropEffects.None
End If
End
Sub 'treeView2_DragEnter

Now we need to handle the actual drop in the DragDrop event. In this example we actually determine where in the tree to drop the text using the position passed in from the DragEvent Arguement:

Listing 3.

Private Sub treeView2_DragDrop(sender As Object, e As System.Windows.Forms.DragEventArgs)
' Get the Data from the DragEventArguement
Dim dummy As String = "hello"
Dim s As String = CStr(e.Data.GetData(dummy.GetType()))
' Parse out the type information
s = s.Substring((s.IndexOf(":") + 1)).Trim()
Position.X = e.X
Position.Y = e.Y
' Convert from Form Coordinates to treeView Client coordinates
Position = treeView2.PointToClient(Position)
' Find the node at this position
Dim DropNode As TreeNode = Me.treeView2.GetNodeAt(Position)
' if the node exists, insert a new node with the dropped string
' into the second tree
If Not (DropNode Is Nothing) Then
Dim
DragNode As New TreeNode(s)
treeView2.Nodes.Insert(DropNode.Index + 1, DragNode)
End If
End
Sub 'treeView2_DragDrop

The ListBox handles drops the same way. The DragEnter Event for the listbox  is set up to handle the DropEffects and the DragDrop event handler handles dropping the text:

Listing 4..

Private Sub listBox1_DragEnter(sender As Object, e As System.Windows.Forms.DragEventArgs)
' Handle the Drag effect when the listbox is entered
If e.Data.GetDataPresent(DataFormats.Text) Then
e.Effect = DragDropEffects.Copy
Else
e.Effect = DragDropEffects.None
End If
End
Sub 'listBox1_DragEnter
Private Sub listBox1_DragDrop(sender As Object, e As System.Windows.Forms.DragEventArgs)
Dim dummy As String = "hello"
' Get the string from the data that was dragged
Dim s As String = CStr(e.Data.GetData(dummy.GetType()))
s = s.Substring((s.IndexOf(":") + 1)).Trim()
Position.X = e.X
Position.Y = e.Y
Position = listBox1.PointToClient(Position)
' Drop the string in the listbox
listBox1.Items.Add(s)
End Sub 'listBox1_DragDrop

share this article :
post comment
 

Hi Mike I got 5 errors during the execution of the project. Would you help me? Error 1 Name 'Position' is not declared. C:\Users\Antonio\Documents\Visual Studio 2005\Projects\DragAndDrop\DragAndDrop\Form1.vb 11 45 DragAndDrop Error 2 Name 'Position' is not declared. C:\Users\Antonio\Documents\Visual Studio 2005\Projects\DragAndDrop\DragAndDrop\Form1.vb 13 7 DragAndDrop Error 3 Name 'Position' is not declared. C:\Users\Antonio\Documents\Visual Studio 2005\Projects\DragAndDrop\DragAndDrop\Form1.vb 14 7 DragAndDrop Error 4 Name 'Position' is not declared. C:\Users\Antonio\Documents\Visual Studio 2005\Projects\DragAndDrop\DragAndDrop\Form1.vb 15 7 DragAndDrop Error 5 Name 'Position' is not declared. C:\Users\Antonio\Documents\Visual Studio 2005\Projects\DragAndDrop\DragAndDrop\Form1.vb 15 41 DragAndDrop

Posted by Anthony Fonseca Dec 07, 2008

I have crated video tutorial about Drag and drop you can watch from here http://vb-heaven.com//wp-admin/post.php?action=edit&post=23

Posted by Prasanna Oct 21, 2008
Nevron Diagram
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
Nevron Diagram
Become a Sponsor