Ich versuche, Drag-and-Drop-Sortierung in Listview auf meinem VBA-Formular zu implementieren. Ich habe viele Lösungen für VB-Formulare gefunden. Aber sie funktionieren nicht in VBA. Ich habe auch einen Artikel für VBA gefunden und es funktioniert fast. Aber Problem ist, dass, wenn ich den Gegenstand ziehe, mein Cursor andere Gegenstände nicht hervorhebt, wenn mouseover. Es hebt nur die erste Zeile hervor, wenn ich den Gegenstand unter die letzte Linie ziehe. Hier ist 2 screenshots für eine bessere Erklärung. Und hier ist der Code:VBA - Listview Sortierung per Drag & Drop
Public Sub LVDragDropSingle(ByRef lvList As ListView, ByVal x As Single,
ByVal y As Single)
'Item being dropped
Dim objDrag As ListItem
'Item being dropped on
Dim objDrop As ListItem
'Item being readded to the list
Dim objNew As ListItem
'Subitem reference in dropped item
Dim objSub As ListSubItem
'Drop position
Dim intIndex As Integer
'Retrieve the original items
Set objDrop = lvList.HitTest(x, y)
Set objDrag = lvList.SelectedItem
If (objDrop Is Nothing) Or (objDrag Is Nothing) Then
Set lvList.DropHighlight = Nothing
Set objDrop = Nothing
Set objDrag = Nothing
Exit Sub
End If
'Retrieve the drop position
intIndex = objDrop.Index
'Remove the dragged item
lvList.ListItems.Remove objDrag.Index
'Add it back into the dropped position
Set objNew = lvList.ListItems.Add(intIndex, objDrag.key, objDrag.Text, objDrag.Icon, objDrag.SmallIcon)
'Copy the original subitems to the new item
If objDrag.ListSubItems.Count > 0 Then
For Each objSub In objDrag.ListSubItems
objNew.ListSubItems.Add objSub.Index, objSub.key, objSub.Text, objSub.ReportIcon, objSub.ToolTipText
Next
End If
'Reselect the item
objNew.Selected = True
'Destroy all objects
Set objNew = Nothing
Set objDrag = Nothing
Set objDrop = Nothing
Set lvList.DropHighlight = Nothing
End Sub
und 2 U-Boote für die Userform:
Private Sub ListView1_OLEDragOver(Data As MSComctlLib.DataObject, Effect As Long, Button As Integer, Shift As Integer, x As Single, y As Single, State As Integer)
Set ListView1.DropHighlight = ListView1.HitTest(x, y)
End Sub
Private Sub ListView1_OLEDragDrop(Data As MSComctlLib.DataObject, Effect As Long, Button As Integer, Shift As Integer, x As Single, y As Single)
Call LVDragDropSingle(ListView1, x, y)
End Sub
Dieser Artikel i hat eine Erklärung gefunden. Schade, dass ich den Link nicht posten kann, da ich nicht mehr als einen Link posten darf.