Microsoft KB Archive/822483

= The ListView control does not support drag-and-drop functionality for items at run time =

Article ID: 822483

Article Last Modified on 11/26/2007

-

APPLIES TO


 * Microsoft Visual Basic 2005
 * Microsoft Visual Basic .NET 2003 Standard Edition
 * Microsoft Visual Basic .NET 2002 Standard Edition
 * Microsoft Visual C# 2005 Express Edition
 * Microsoft Visual C# .NET 2003 Standard Edition
 * Microsoft Visual C# .NET 2002 Standard Edition

-





SYMPTOMS
If you add items to the ListView control on a Microsoft Windows form, you cannot reorder the items by dragging the items in the ListView control at run time.



CAUSE
This problem occurs because the ListView control does not support drag-and-drop functionality for the items at run time.



WORKAROUND
To work around this problem, provide drag-and-drop functionality to the ListView control as follows:   Add the following code to the ListView1_ItemDrag event handler.

Microsoft Visual Basic .NET or Microsoft Visual Basic 2005 code 'Begins a drag-and-drop operation in the ListView control. ListView1.DoDragDrop(ListView1.SelectedItems, DragDropEffects.Move) Microsoft Visual C# .NET or Microsoft Visual C# 2005 code //Begins a drag-and-drop operation in the ListView control. listView1.DoDragDrop(listView1.SelectedItems, DragDropEffects.Move);   Add the following code to the ListView1_DragEnter event handler.

Visual Basic .NET or Visual Basic 2005 code Dim i As Integer For i = 0 To e.Data.GetFormats.Length - 1 If e.Data.GetFormats(i).Equals(&quot;System.Windows.Forms.ListView+SelectedListViewItemCollection&quot;) Then 'The data from the drag source is moved to the target. e.Effect = DragDropEffects.Move End If Next Visual C# .NET or Visual C# 2005 code int len=e.Data.GetFormats.Length-1 ; int i; for (i = 0 ; i<=len ;i++) {   if (e.Data.GetFormats[i].Equals(&quot;System.Windows.Forms.ListView+SelectedListViewItemCollection&quot;)) {       //The data from the drag source is moved to the target. e.Effect = DragDropEffects.Move; } }   Add the following code to the ListView1_DragDrop event handler.

Visual Basic .NET or Visual Basic 2005 code 'Return if the items are not selected in the ListView control. If ListView1.SelectedItems.Count = 0 Then Return 'Returns the location of the mouse pointer in the ListView control. Dim p As Point = ListView1.PointToClient(New Point(e.X, e.Y)) 'Obtain the item that is located at the specified location of the mouse pointer. Dim dragToItem As ListViewItem = ListView1.GetItemAt(p.X, p.Y) If dragToItem Is Nothing Then Return 'Obtain the index of the item at the mouse pointer. Dim dragIndex As Integer = dragToItem.Index Dim i As Integer Dim sel(ListView1.SelectedItems.Count) As ListViewItem For i = 0 To ListView1.SelectedItems.Count - 1 sel(i) = ListView1.SelectedItems.Item(i) Next For i = 0 To ListView1.SelectedItems.Count - 1 'Obtain the ListViewItem to be dragged to the target location. Dim dragItem As ListViewItem = sel(i) Dim itemIndex As Integer = dragIndex If itemIndex = dragItem.Index Then Return If dragItem.Index < itemIndex Then itemIndex = itemIndex + 1 Else itemIndex = dragIndex + i   End If    'Insert the item in the specified location. Dim insertitem As ListViewItem = dragItem.Clone ListView1.Items.Insert(itemIndex, insertitem) 'Removes the item from the initial location while 'the item is moved to the new location. ListView1.Items.Remove(dragItem) Next Visual C# .NET or Visual C# 2005 code //Return if the items are not selected in the ListView control. if(listView1.SelectedItems.Count==0) {  return; } //Returns the location of the mouse pointer in the ListView control. Point cp = listView1.PointToClient(new Point(e.X, e.Y)); //Obtain the item that is located at the specified location of the mouse pointer. ListViewItem dragToItem = listView1.GetItemAt(cp.X, cp.Y); if(dragToItem==null) {   return; } //Obtain the index of the item at the mouse pointer. int dragIndex = dragToItem.Index; ListViewItem[] sel=new ListViewItem [listView1.SelectedItems.Count]; for(int i=0; i<=listView1.SelectedItems.Count-1;i++) {    sel[i]=listView1.SelectedItems[i]; } for(int i=0; i<sel.GetLength(0);i++) {    //Obtain the ListViewItem to be dragged to the target location. ListViewItem dragItem = sel[i]; int itemIndex = dragIndex; if(itemIndex==dragItem.Index) {       return; }  if(dragItem.Index On the Build menu, click Build Solution. On the Debug menu, click Start.

<div class="status_section">

STATUS
This behavior is by design.

<div class="moreinformation_section">

Steps to Reproduce the Behavior

 * 1) Start Microsoft Visual Studio .NET or Microsoft Visual Studio 2005.
 * 2) Create a new Windows application by using Visual Basic .NET, Visual Basic 2005, Visual C# 2005, or Visual C# .NET.

By default, Form1 is created.
 * 1) Add the ListView control to Form1.
 * 2) Right-click ListView1, and then click Properties.
 * 3) In the Properties dialog box, click the Items property, and then click the ellipsis button (...).
 * 4) In the ListViewItem Collection Editor dialog box, click Add four times.
 * 5) Set the Text property of the first ListViewItem control to Item1, set the Text property of the second ListViewItem control to Item2, set the Text property of the third ListViewItem control to Item3, and then set the Text property of the fourth ListViewItem control to Item4.
 * 6) In the ListViewItem Collection Editor dialog box, click OK.
 * 7) In the Properties dialog box of ListView1, set the View property to List.
 * 8) Set the AllowDrop property of ListView1 to True.
 * 9) On the Build menu, click Build Solution.
 * 10) On the Debug menu, click Start.
 * 11) Try to drag Item1 to a new location in the ListView control.

You cannot drag Item1.

<div class="references_section">