Results 1 to 2 of 2

Thread: Drag-all in a ListView, and how to constrain to a box

  1. #1

    Thread Starter
    Fanatic Member scr0p's Avatar
    Join Date
    Oct 2002
    Location
    VA
    Posts
    720

    Drag-all in a ListView, and how to constrain to a box

    I have multi select on, and I can select as much items as I want, but how do I drag a lot at once?

    Another question (listview), How do I make it so you can only drag in the boxed area of the Listview, by default, the user can keep dragging the item and the listview just gets scroll bars.
    asdf

  2. #2
    Guru Aaron Young's Avatar
    Join Date
    Jun 1999
    Location
    Red Wing, MN, USA
    Posts
    2,177
    Here's a simple method for utilizing multi drag drop capability.
    By using the OLE Drag/Drop functionality, the control will behave
    like it would in any other Windows application.
    VB Code:
    1. Private Sub Form_Load()
    2.   Dim lIndex As Long
    3.  
    4.   ' Generate 10 Items in the Listview
    5.   With ListView1
    6.    
    7.     ' Turn on MultiSelect and OLEDragMode
    8.     .MultiSelect = True
    9.     .OLEDragMode = ccOLEDragAutomatic
    10.    
    11.     For lIndex = 1 To 10
    12.       .ListItems.Add , "Key" & lIndex, "Item " & lIndex
    13.     Next
    14.    
    15.   End With
    16.  
    17.   ' Turn on OLEDropMode for the Listbox
    18.   List1.OLEDropMode = vbOLEDropManual
    19.  
    20. End Sub
    21.  
    22. Private Sub ListView1_OLESetData(Data As MSComctlLib.DataObject, DataFormat As Integer)
    23.  
    24.   ' Store a list of Item "Key" values as the Drag Data
    25.   Dim oItem As ListItem
    26.   Dim sList As String
    27.  
    28.   ' Step through each selected ListView Item
    29.   ' Adding it's Unique Key to the Delimited List
    30.   For Each oItem In ListView1.ListItems
    31.     If oItem.Selected Then
    32.       sList = sList & "," & oItem.Key
    33.     End If
    34.   Next
    35.  
    36.   ' Set the drag Data to the Delimited string, inserting
    37.   ' The Listview name as the 1st element for identification.
    38.   Call Data.SetData("ListView1" & sList, vbCFText)
    39. End Sub
    40.  
    41. Private Sub List1_OLEDragDrop(Data As DataObject, Effect As Long, Button As Integer, Shift As Integer, X As Single, Y As Single)
    42.  
    43.   ' When something is dropped on the Listbox..
    44.   Dim vItem As Variant
    45.   Dim sSource As String
    46.   Dim lIndex As Long
    47.  
    48.   ' Check to see if the Data is "Text"
    49.   If Data.GetFormat(vbCFText) Then
    50.    
    51.     ' Extract the text and split it using a comma delimiter
    52.     sSource = Data.GetData(vbCFText)
    53.     vItem = Split(sSource, ",")
    54.    
    55.     ' If the 1st item in the delimited list is the name of the ListView
    56.     ' Then it's a valid OLE Drag Source
    57.     If vItem(0) <> "ListView1" Then Exit Sub
    58.    
    59.     ' Clear the Listbox
    60.     List1.Clear
    61.    
    62.     ' Step through each item in the delimited list
    63.     ' Using the Key value to locate the appropriate ListView Item
    64.     ' And add it's text to the Listbox
    65.     For lIndex = 1 To UBound(vItem)
    66.       List1.AddItem ListView1.ListItems(vItem(lIndex)).Text
    67.     Next
    68.    
    69.   End If
    70. End Sub
    The only drawback would be that you have limited control over
    the drag icon, plus the data is also available to any other
    OLE Drag/Drop enabled application.

    You could implement something similar using the application Drag/Drop
    functionality, however it would be a bit more work - depends on your needs.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width