Drag and Drop
Lets the user of a ListView drag its items to another place or program, shows where a drop would go, and takes the files dropped from Windows Explorer.
Private Sub ListView1_ItemDrag(ByVal Item As LvwListItem, ByVal Button As Integer)
If Button = vbLeftButton Then ListView1.OLEDrag ' drags Item
End Sub
Private Sub ListView1_OLEStartDrag(Data As DataObject, AllowedEffects As Long)
Data.SetData ListView1.OLEDraggedItem.Text, vbCFText
AllowedEffects = vbDropEffectMove
End Sub
Private Sub ListView1_OLEDragOver(Data As DataObject, Effect As Long, Button As Integer, Shift As Integer, X As Single, Y As Single, State As Integer)
Dim Item As LvwListItem, After As Boolean
Set Item = ListView1.HitTestInsertMark(X, Y, After)
Set ListView1.InsertMark(After) = Item
End Sub
Dragging items
The user drags an item by pressing a mouse button on it and moving the mouse, which raises ItemDrag. With OLEDragMode vbOLEDragAutomatic the control then starts an OLE drag of the item at once, with the item’s text as the data. With vbOLEDragManual, the default, the program starts it, if it wants one, by calling OLEDrag in the handler of ItemDrag. During the drag OLEDraggedItem is the item.
Within the control, in the icon, small icon and tile views, the dragged item follows the pointer and stays where it is dropped, unless Arrange arranges the items automatically. OLEDragDropScroll scrolls the control while the pointer is near one of its edges during a drag.
The rest is the OLE drag and drop of the package’s other controls: OLEDropMode, OLEDragOver, OLEDragDrop and the events of the source.
Showing where a drop goes
The control can show the place of a drop in two ways. DropHighlight highlights an item, for a drop onto it, such as a file dropped on a folder. InsertMark draws a line before or after an item, for a drop between two items; HitTestInsertMark gives the item and the side for a point of OLEDragOver. The program sets them, and sets them back to Nothing when the drag leaves the control or ends.
Dropped files
With AllowDropFiles True and OLEDropMode OLEDropModeNone, files dropped on the control from Windows Explorer raise DropFiles with their paths. With OLEDropModeManual they arrive as OLE data in OLEDragDrop instead.
Private Sub ListView1_DropFiles(FileList As Variant, ByVal X As Single, ByVal Y As Single)
Dim i As Long
For i = LBound(FileList) To UBound(FileList)
ListView1.ListItems.Add , , FileList(i)
Next
End Sub
Properties
AllowDropFiles
Whether files dropped from Windows Explorer raise DropFiles. Boolean. Default: False. It has an effect while OLEDropMode is OLEDropModeNone.
DropHighlight
The item highlighted as the target of a drop. An LvwListItem, or Nothing. Setting it highlights that item and takes the highlight from any other; Nothing highlights none.
InsertMark
The item at which the insertion mark is drawn, a line between two items. An LvwListItem, or Nothing for no mark. It has an effect when the program has visual styles.
Syntax: object.InsertMark( [ After ] ) [ = item ]
- After
- optional A Boolean. In an assignment, True draws the mark after the item and False, the default, before it. In a reading, a variable that receives where the mark is.
Dim After As Boolean
With ListView1
.View = LvwViewReport
.ColumnHeaders.Add , , "Name"
.ListItems.Add , , "alpha"
.ListItems.Add , , "beta"
Set .InsertMark(True) = .ListItems(1) ' between alpha and beta
Debug.Print .InsertMark(After).Text; After
Set .InsertMark = Nothing
Debug.Print TypeName(.InsertMark)
End With
' Output:
' alphaTrue
' Nothing
InsertMarkColor
The color of the insertion mark. OLE_COLOR. Default: vbBlack. It has an effect when the program has visual styles.
OLEDragDropScroll
Whether the control scrolls while the pointer of an OLE drag is near one of its edges. Boolean. Default: True.
OLEDragDropScrollOrientation
In which directions OLEDragDropScroll scrolls. A member of CCScrollOrientationConstants. Default: CCScrollOrientationBoth. Another value raises run-time error 380.
OLEDraggedItem
The item that an OLE drag started at the control is dragging. An LvwListItem, read-only, or Nothing when there is none.
OLEDragMode
Whether the control starts an OLE drag by itself when the user drags an item. A member of OLEDragConstants: vbOLEDragManual (the default) or vbOLEDragAutomatic. Another value raises run-time error 380. See Dragging items.
Methods
HitTestInsertMark
Returns the item at which an insertion mark for a point belongs, or Nothing, and whether the mark goes after it. It needs visual styles; without them it returns Nothing.
Syntax: object.HitTestInsertMark( X, Y [, After ] ) As LvwListItem
- X, Y
- required Single: the point, measured from the control’s top left corner in the scale units of its container, such as the point of OLEDragOver.
- After
- optional A Boolean variable that receives True when the mark goes after the item.
OLEDrag
Starts an OLE drag with the control as its source, as on the other controls. Called in the handler of ItemDrag, the drag takes the item the user is dragging, and OLEDraggedItem returns it.
Syntax: object.OLEDrag
Events
DropFiles
Raised when files from Windows Explorer are dropped on the control, while AllowDropFiles is True.
Syntax: object_DropFiles( FileList As Variant, ByVal X As Single, ByVal Y As Single )
- FileList
- An array of String, from 0: the full path of each file or folder dropped.
- X, Y
- Where they were dropped, measured from the control’s top left corner in the scale units of its container.
ItemDrag
Raised when the user starts to drag an item with the left or the right mouse button.
Syntax: object_ItemDrag( ByVal Item As LvwListItem, ByVal Button As Integer )
- Item
- The LvwListItem.
- Button
- vbLeftButton or vbRightButton. With vbOLEDragAutomatic the control starts a drag for the left button only.
OLEStartDrag
Raised when an OLE drag starts at the control, as on the other controls. With OLEDragMode vbOLEDragAutomatic, Data already holds the text of the dragged item, and AllowedEffects is vbDropEffectCopy plus vbDropEffectMove; the handler can change both. A handler that sets AllowedEffects to vbDropEffectNone ends the drag.
Syntax: object_OLEStartDrag( Data As DataObject, AllowedEffects As Long )
See Also
- ListView control
- Common members – the OLE drag and drop of every control
- DataObject class – what an OLE drag carries