Re: Listview Drag & Drop



"Clair Blair" <noone@xxxxxxxx> wrote in message
news:Pb-dnXRb6to48THaRVnyhgA@xxxxxxxxx

After struggling with this for a few days, I am getting quite frustrated
now. All I want to do is this:

1). Display a form with a ListView containing a list of items (I can do
this)

2). Drag an item from the list (without removing it from the list)
- Can't work this out yet - Aarrrgh !!!

I believe vbOLEDragAutomatic is your enemy here. Set it to manual and handle
the dragging yourself. A crude way is to simply start dragging the moment
the user does a MouseDown (which I admit is how I do it). The more
sophisticated way is to set a flag on MouseDown and then check to see if the
flag is still set during MouseMove and turn on dragging if the mouse has
moved a sufficient distance. Here's a sample of (crude!) code from one of my
apps:

Private Sub lvwView_MouseDown(Button As Integer, Shift As Integer, X As
Single, y As Single)
' Get a reference to the ListItem being dragged
Set m_lsiDrag = lvwView.HitTest(X, y)
If Not (m_lsiDrag Is Nothing) Then
m_lsiDrag.Selected = True
' lvwView.DragIcon = m_lsiDrag.CreateDragImage
lvwView.OLEDrag
End If
End Sub

You also need to handle OLEStartDrag and OLECompleteDrag. In StartDrag
you'll generally fill in the Data parameter with whatever information you
need to identify the data you're dragging. This info will be passed on to
drag targets so they can decide whether they'll even allow it to be dropped.
Here's mine:

Private Sub lvwView_OLEStartDrag(Data As MSComCtlLib.DataObject,
AllowedEffects As Long)
With Data
.Clear
' Tag the data so that the coupon form doesn't try to accept data
dragged
' from other applications
.SetData "CLDrag:" & m_lsiDrag.Tag, vbCFText
End With
AllowedEffects = vbDropEffectMove
End Sub

In my case I'm moving something from one place to another, hence the
vbDropEffectMove above. You want to copy, so you'd use vbDropEffectCopy.
Note that this only has an effect on the display of the pointer; it's your
code that will ultimately move/copy/whatever the item.

3). Set the icon to display when an item is being dragged - for some
strange reason, setting the DragIcon property for the listView has NO
effect - what gives ?

Hmmm...I see to recall that setting a nice icon in VB is kind of a Holy
Grail of drag & drop, but I might be thinking to the Windows XP behavior of
making the "icon" a large, ghosted picture of the item(s) you're dragging.
(Notice now I commented out the CreateDragImage in the code above. It never
worked for me. This also demonstrates that I'm lazy and leave old code lying
around....)

4). Trap the drag event on the form, so that when the item is dropped on
the form, I can display a message box with the text that was dropped
- Can't work this out yet - Aarrrgh !!!

Looking at some old code, I believe you want to set the OLEDropMode of the
form to 1 - Manual. Then you handle the OLEDragOver and OLEDragDrop methods.


.