Windows users fall into two general categories: those who prefer to use the keyboard and those who prefer to use the mouse. Programmers have been taught to look after the needs of keyboard users by providing access keys (the underlined letter in a command or menu) and shortcuts (such as a CTRL + letter combination), but the needs of mouse users have largely been ignored. Programmers tend to primarily be keyboard users, so the emphasis on keyboard-oriented features is understandable, but every programmer should consider providing mouse support as well.

One thing that mouse users expect is the ability to drag and drop. If you look at most major applications or at Windows itself, drag and drop is everywhere. For example, users are accustomed to dragging and dropping files in the Windows Explorer and to dragging and dropping text in Microsoft Word.

Despite these expectations, few programmers provide drag-and-drop capability in their applications �� most likely because implementing drag and drop appears to be much more difficult than it actually is. This example demonstrates how to perform drag-and-drop operations in a Windows Forms Application.

Running the Sample

To see the result, press the left key of your mouse down, and drag an item from the ListBox(on the right side) and drop it to the TreeView(on the left side), then release your mouse.

The items are added into the TreeView control.

Using the Code

1.      Enable dropping on the destination control(it's TreeView in this example) by setting AllowDrop property to true.

      ' Enable dropping on the TreeView
      Me.treeView1.AllowDrop = True


2.      Handle the MouseDown event on the source control(here is ListBox) to start  the drag operation. And call the DoDragDrop method to enable data to be collected when dragging begins.

   Private Sub listBox1_MouseDown(ByVal sender As Object, ByVal e As MouseEventArgs)
       ' In the MouseDown event for the ListBox where the drag will begin, 
       ' use the DoDragDrop method to set the data to be dragged 
       ' and the allowed effect dragging will have.
       If (Not Me.listBox1.SelectedItem Is Nothing) Then
           Me.listBox1.DoDragDrop(Me.listBox1.SelectedItem, DragDropEffects.Copy)
       End If
   End Sub


3.      Handle the DragEnter event on the destination control to set the effect  that will happen when the drop occurs.

   Private Sub treeView1_DragDrop(ByVal sender As Object, ByVal e As DragEventArgs)
       ' In the DragDrop event for the TreeView where the drop will occur, 
       ' use the GetData method to retrieve the data being dragged.
       Dim item As String = CStr(e.Data.GetData(e.Data.GetFormats()(0)))
       ' Add the item strib
   End Sub


4.      Handle the DragDrop event on the destination control to retrieve the data dragged from the source control.

   Private Sub treeView1_DragEnter(ByVal sender As Object, ByVal e As DragEventArgs)
       ' Sets the effect that will happen when the drop occurs to a value 
       ' in the DragDropEffects enumeration.
       e.Effect = DragDropEffects.Copy
   End Sub


More Information

Windows Forms General FAQ.

Performing Drag-and-Drop Operations in Windows Forms