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:
- Start Microsoft Visual Studio 2005 or Microsoft Visual Studio .NET.
- On the File menu, point to
New, and then click Project.
- Under Project Types, click Visual
Basic Projects.
Note In Visual Studio 2005, click Visual Basic under Project Types. - Under Templates, click Windows
Application.
- In the Name text box, type
Drag_Application, and then click OK.
By default, Form1 is created. - Add a ListView control to Form1.
ListView1 is created. - Right-click ListView1, and
then click Properties.
- In the Properties dialog box, click the
Items property, and then click the ellipses
(...) button.
- In the ListViewItem Collection Editor
dialog box, click Add four times.
- 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.
- In the ListViewItem Collection Editor
dialog box, click OK.
- In the Properties dialog box of
ListView1, set the View property to
List.
- Add a Label control to Form1.
By
default, Label1 is created. - Put Label1 on the left side of ListView1 or above ListView1.
- In the Properties dialog box, set the
Text property of Label1 to
Drag the Item from this ListView.
- 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:
- Start Microsoft Visual Studio 2005 or Microsoft Visual Studio .NET.
- On the File menu, point to
New, and then click Project.
- Under Project Types, click Visual
Basic Projects.
Note In Visual Studio 2005, click Visual Basic under Project Types. - Under Templates, click Windows
Application.
- In the Name text box, type
Drop_Application, and then click OK.
By default, Form1 is created. - Add a ListView control to Form1.
By default, ListView1 is created. - Right-click ListView1, and then click
Properties.
- In the Properties dialog box, set the
AllowDrop property to True.
- Set the View property of
ListView1 to List.
- Add a Label control to Form1.
By
default, Label1 is created. - Put Label1 on the left side of
ListView1 or above ListView1.
- In the Properties dialog box, set the
Text property of Label1 to Drop the
Item in this ListView.
- 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
- 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
- On the Build menu of the
Drag_Application Windows application, click Build
Solution.
- On the Debug menu, click
Start.
- On the Build menu of the
Drop_Application Windows application, click Build
Solution.
- On the Debug menu, click
Start.
- 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.