How to provide drag-and-drop functionality in Microsoft Windows applications by using Visual Basic 2005 or Visual Basic .NET (822482)



The information in this article applies to:

  • Microsoft Visual Basic 2005
  • Microsoft Visual Basic .NET (2003)
  • Microsoft Visual Basic .NET (2002)

SUMMARY

This step-by-step article describes how to provide drag-and-drop functionality in Microsoft Windows applications by using Microsoft Visual Basic 2005 or Microsoft Visual Basic .NET. The sample in this article describes how to drag items from the ListView control of your current application to a ListView control that is located on a Windows Form of a second application.

Requirements

The following list outlines the recommended hardware, software, network infrastructure, and service packs that you need:
  • Visual Basic 2005 or Visual Basic .NET
This article assumes that you are familiar with the following topics:
  • Windows Forms ListView control
  • Windows Forms event handling

Using the ItemDrag event to set the data that you want to drag

You can use the ItemDrag event of the ListView control to set the data that you want to drag. When you call the DoDragDrop method for the ListView control, you initiate the dragging. The DoDragDrop method takes the data parameter and the allowedEffects parameter. The data parameter contains the data to be passed to the target ListView control. The allowedEffects parameter specifies what operations (copying, moving, or both) are permitted.

To help support the drag functionality for the ListView control, follow these steps:
  1. Start Microsoft Visual Studio 2005 or Microsoft Visual Studio .NET.
  2. On the File menu, point to New, and then click Project.
  3. Under Project Types, click Visual Basic Projects.

    Note In Visual Studio 2005, click Visual Basic under Project Types.
  4. Under Templates, click Windows Application.
  5. In the Name text box, type Drag_Application, and then click OK.

    By default, Form1 is created.
  6. Add a ListView control to Form1.

    ListView1 is created.
  7. Right-click ListView1, and then click Properties.
  8. In the Properties dialog box, click the Items property, and then click the ellipses (...) button.
  9. In the ListViewItem Collection Editor dialog box, click Add four times.
  10. Set the Text property of the first ListViewItem to Item1, set the Text property of the second ListViewItem to Item2, set the Text property of the third ListViewItem to Item3, and then set the Text property of the fourth ListViewItem to Item4.
  11. In the ListViewItem Collection Editor dialog box, click OK.
  12. In the Properties dialog box of ListView1, set the View property to List.
  13. Add a Label control to Form1.

    By default, Label1 is created.
  14. Put Label1 on the left side of ListView1 or above ListView1.
  15. In the Properties dialog box, set the Text property of Label1 to Drag the Item from this ListView.
  16. Add the following code to the ListView1_ItemDrag event handler.
    Dim myItem As ListViewItem
    'Create an array of strings. 
    Dim myItems(ListView1.SelectedItems.Count - 1) As String
    Dim i As Integer = 0
    'Loop though the SelectedItems collection of the ListView control.
    For Each myItem In ListView1.SelectedItems
       'Add the Text of the ListViewItem to the array. 
       myItems(i) = myItem.Text
       i = i + 1
    Next
    'DoDragDrop begins the drag-and-drop operation.
    'The data to be dragged is the array of strings.
    ListView1.DoDragDrop(myItems, DragDropEffects.Move)
    Dim j As ListViewItem
    For Each j In ListView1.SelectedItems
        'Remove the ListViewItem from the ListView control.
        ListView1.Items.Remove(j)
    Next

Using the DragEnter event and the DragDrop event to drop the data

The ListView control uses the DragEnter drag-and-drop event and the DragDrop drag-and-drop event to drop the data in the second ListView control. The DragEnter event is raised when you drag an object in the bounds of the control. The DragEnter event determines whether the object that you drag is in the string array format.

The DragDrop event occurs when you release the object on the control. Code is written in the event handler of the DragDrop event to retrieve the data. You can use the Data object to retrieve the data. The GetData method of the Data object returns the data in the format that you specify. You can add this data to the ListView control.

To help support the drop functionality for the ListView control, follow these steps:
  1. Start Microsoft Visual Studio 2005 or Microsoft Visual Studio .NET.
  2. On the File menu, point to New, and then click Project.
  3. Under Project Types, click Visual Basic Projects.

    Note In Visual Studio 2005, click Visual Basic under Project Types.
  4. Under Templates, click Windows Application.
  5. In the Name text box, type Drop_Application, and then click OK.

    By default, Form1 is created.
  6. Add a ListView control to Form1.

    By default, ListView1 is created.
  7. Right-click ListView1, and then click Properties.
  8. In the Properties dialog box, set the AllowDrop property to True.
  9. Set the View property of ListView1 to List.
  10. Add a Label control to Form1.

    By default, Label1 is created.
  11. Put Label1 on the left side of ListView1 or above ListView1.
  12. In the Properties dialog box, set the Text property of Label1 to Drop the Item in this ListView.
  13. Add the following code to the ListView1_DragEnter event handler.
    'Check for the DataFormat string array.
    If e.Data.GetDataPresent("System.String[]") Then
       'If the data stored is a string array,
       'set the Effect of drag-and-drop operation to Move.
       e.Effect = DragDropEffects.Move
    Else
       'Else set the Effect of drag-and-drop operation to None.
       e.Effect = DragDropEffects.None
    End If 
  14. Add the following code to the ListView1_DragDrop event handler.
     'Retrieve the data in the string array format.
    Dim myText() As String = e.Data.GetData("System.String[]")
    Dim i As Integer
    For i = 0 To myText.Length - 1
       'Add the dragged items to the ListView control.
       ListView1.Items.Add(myText(i))
    Next
Note You can provide drag-and-drop functionality between two ListView controls on the same Windows Form with the same code, as described in the DragEnter event handler and in the DragDrop event handler in this sample. The ListView control uses the code in the event handlers of the DragDrop event and the DragEnter event to provide the drag-and-drop functionality for the ListView control that you drag the data to.

Verify that it works

  1. On the Build menu of the Drag_Application Windows application, click Build Solution.
  2. On the Debug menu, click Start.
  3. On the Build menu of the Drop_Application Windows application, click Build Solution.
  4. On the Debug menu, click Start.
  5. Drag items from the current ListView control to the second ListView control.

Troubleshooting

  • To support the drop functionality, set the AllowDrop property of the ListView control to True. Otherwise, you cannot drop the item that you drag on the ListView control.
  • To support the drag functionality and to set the data to be dragged, call the DoDragDrop method in the ItemDrag event of the ListView control.

REFERENCES

For more information, visit the following Microsoft Developer Network (MSDN) Web sites:

Modification Type:MinorLast Reviewed:10/3/2006
Keywords:kbvs2005applies kbvs2005swept kbHOWTOmaster kbhowto kbForms kbDragDrop kbWindowsForms KB822482 kbAudDeveloper