ListViews drag-drop + reorder

mikemikk

Member
Joined
Jul 3, 2007
Messages
10
Programming Experience
10+
Hello, I have a windows form with two listview controls. I have finally gotten drag-n-drip working for the SourceListView to the DestListView. But after a drag operation is completed, if I then select a single item in the DestListView, all Items become selected.

Has anyone else noticed this... or is it just me. :confused:
 
I just tried listview.selecteditems.clear, and that left me with a weirder problem. The destination listview is also "orderable" using drag n drip (you can move items around into a different order). After the drag operation, DestListView does not respond to drag operations until I select another control, like the SourceListView, *then* I can select items in the DestListView and drag them into order.

weird...
 
Ok, I also tried SourceListView.Select. But I think I have a better description of what's going on:

After a drag operation from SourceListView to DestListView, DestListView does not fire a ItemDrag when you attempt to drag an item to a different position. But if you select any item in the SourceListView, DestListView begins to start triggering ItemDrag events like it should. Then drag n drip reordering of the items in DestListView is possible. But at least one item MUST be selected in SourceListView.

That make sense?
 
I pulled all of the drag n drip stuff out of the sub components classes, Bubbled-up the Drag events, and now handle everything from the main form. The previous problem has disappeared, but enough new ones have come up to justify another post...
 
ListView to ListView DragNDrop Problem

Hello, I have two ListView components on a form between which I have DragNDrop working. However, there is a funny issue: After dragging items between listview1 to listview2, I cannot drag anything in listview2 (re-order) until I first select *any* item in listview1 (single left click). Once I select an item in Listview1, listview2's ItemDrag starts responding to drags.

Any hints?
 
Here's a sample to DragDrop one or more selected items between or within Listviews, both Copy and Move operation supported:
VB.NET:
Private Sub ListView1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) _
Handles ListView1.MouseMove, ListView2.MouseMove

    If e.Button = Windows.Forms.MouseButtons.Left Then
        Dim lv As ListView = sender
        If lv.DoDragDrop(lv.SelectedItems, DragDropEffects.Move Or DragDropEffects.Copy) = DragDropEffects.Move Then
            For Each lvi As ListViewItem In lv.SelectedItems
                lv.Items.Remove(lvi)
            Next
        End If
    End If
End Sub

VB.NET:
Private Sub ListView1_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) _
Handles ListView1.DragEnter, ListView2.DragEnter

    If e.Data.GetDataPresent(GetType(ListView.SelectedListViewItemCollection)) Then
        Const CTRL As Integer = 8
        If (e.KeyState And CTRL) = CTRL Then
            e.Effect = DragDropEffects.Copy
        Else
            e.Effect = DragDropEffects.Move
        End If
    End If
End Sub

VB.NET:
Private Sub ListView1_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) _
Handles ListView1.DragDrop, ListView2.DragDrop

    Dim lv As ListView = sender
    Dim pt As Point = lv.PointToClient(Cursor.Position)
    Dim hit As ListViewHitTestInfo = lv.HitTest(pt)
    Dim sel As ListView.SelectedListViewItemCollection
    sel = e.Data.GetData(GetType(ListView.SelectedListViewItemCollection))
    If hit.Item Is Nothing Then
        For Each lvi As ListViewItem In sel
            lv.Items.Add(lvi.Clone)
        Next
    Else
        For Each lvi As ListViewItem In sel
            lv.Items.Insert(hit.Item.Index, lvi.Clone)
        Next
    End If
End Sub
 
Back
Top