Results 1 to 8 of 8

Thread: [RESOLVED] List drag&drop - help with resorting index of matching string

  1. #1

    Thread Starter
    Member
    Join Date
    Jun 2007
    Posts
    54

    Resolved [RESOLVED] List drag&drop - help with resorting index of matching string

    Hi,
    I found a code on the internet to preform drag&drop in a list box.
    But now i have problem, and i've been breaking my head on this for the past hour...

    When i populate the list, i read an .ini file.
    The list items are the keys and each key has two values that goes into a global string called: strList(99,2) (the third value in the string is determined on run time).

    The list item and the string shares the same index number, and here is my problem:
    if i move the list items around, then the info stored in the string, will no longer match to the list item, since the index in the string remains as originally read one by one from the .ini file.

    So what i need to do is to find the new list item location, and somehow rearrange the string stored info to match the new indexes.
    I tried all kind of ways, but i could not make this happen.

    How can i put into code, so that after the list item is moved to it's new location, the string will rearrange accordingly?

    I really need your help... Thanks allot ahead!

  2. #2
    PowerPoster Ellis Dee's Avatar
    Join Date
    Mar 2007
    Location
    New England
    Posts
    3,530

    Re: List drag&drop - help with resorting index of matching string

    You mean keep the string array synched up with the listview after each drag/drop operation?

    When the user grabs an item for dragging, store the associated array index (key) to a module level variable for use later.

    Once the user successfully drops an item, identify the array index that got dropped onto. Then send the previously saved drag index and the newly acquired drop index to something like the following function:
    Code:
    ' plngDragIndex = Array row index of item being dragged
    ' plngDropIndex = Array row index of location being dropped to
    Private Sub SyncArray(plngDragIndex As Long, plngDropIndex As Long)
        Dim lngDirection As Long
        Dim strTemp(2)
        Dim i As Long
        Dim c As Long
        
        ' Save dragged item to temp var 
        For c = 0 To 2
            strTemp(c) = strList(plngDragIndex, c)
        Next
        ' Identify direction from dragged item to drop target (up or down)
        If plngDragIndex > plngDropIndex Then lngDirection = -1 Else lngDirection = 1
        ' Move array items over one spot
        For i = plngDragIndex To plngDropIndex - lngDirection Step lngDirection
            For c = 0 To 2
                strList(i, c) = strList(i + lngDirection, c)
            Next
        Next
        ' Save target to new home
        For c = 0 To 2
            strList(plngDropIndex, c) = strTemp(c)
        Next
    End Sub
    Last edited by Ellis Dee; Sep 11th, 2009 at 10:23 AM.

  3. #3
    PowerPoster Ellis Dee's Avatar
    Join Date
    Mar 2007
    Location
    New England
    Posts
    3,530

    Re: List drag&drop - help with resorting index of matching string

    Okay, finished editing. That code is freehand so I apologize for any bugs, and I'm a little fuzzy on what you actually want to do. Let me know if you have any questions or if I misunderstood the problem.

  4. #4

    Thread Starter
    Member
    Join Date
    Jun 2007
    Posts
    54

    Re: List drag&drop - help with resorting index of matching string

    thank you so much Ellis Dee, that's exactly what i meant..
    So i can store the current list index to a global temp integer when i first start dragging, but how can i tell what is the index at the drop?

  5. #5
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758

    Re: List drag&drop - help with resorting index of matching string

    The list item and the string shares the same index number, and here is my problem:
    if i move the list items around, then the info stored in the string, will no longer match to the list item, since the index in the string remains as originally read one by one from the .ini file

    So what i need to do is to find the new list item location, and somehow rearrange the string stored info to match the new indexes.
    None of that is necesary if you store the array Index of each Item in its ItemData property. By utilizing ItemData, no matter where you move an Item you can always get to its array data.

    Set the ListBox Sorted propety to True and run the following code.
    Because the listbox is sorted, A (array index 4) will appear at ListIndex 0.

    Clicking on A will print out - 0 A 4
    Clicking on E will print out - 4 E 0


    Code:
    Dim aData(4, 1) As String
    
    Private Sub Form_Load()
        aData(0, 0) = "E"
        aData(0, 1) = "0"
        
        aData(1, 0) = "D"
        aData(1, 1) = "1"
        
        aData(2, 0) = "C"
        aData(2, 1) = "2"
        
        aData(3, 0) = "B"
        aData(3, 1) = "3"
        
        aData(4, 0) = "A"
        aData(4, 1) = "4"
        
        Dim i As Long
        For i = 0 To 4
           List1.AddItem aData(i, 0)
           List1.ItemData(List1.NewIndex) = i
        Next
    
    End Sub
    
    Private Sub List1_Click()
        Debug.Print List1.ListIndex, aData(List1.ItemData(List1.ListIndex), 0), aData(List1.ItemData(List1.ListIndex), 1)
    End Sub

  6. #6

    Thread Starter
    Member
    Join Date
    Jun 2007
    Posts
    54

    Re: List drag&drop - help with resorting index of matching string

    brucevde,
    Do i have to sort the list box for this to work?
    Because now the list box is unsorted on purpose, because the data that is read from the .ini file is already at a certain order (non alphabetic)...

    Ellis Dee,
    your solution works! Thank you so much.
    Last edited by bzemer; Sep 11th, 2009 at 06:36 PM.

  7. #7
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758

    Re: [RESOLVED] List drag&drop - help with resorting index of matching string

    Do i have to sort the list box for this to work?
    No.
    In my example, the Array data is sorted descending and the ListBox is sorted ascending to prove the point - There is no reason to keep the ListBox and Array indexes in sync when a ListBox item is moved.

    But then again I think I maybe misunderstanding the complete requirements.
    Last edited by brucevde; Sep 11th, 2009 at 06:00 PM.

  8. #8
    PowerPoster Ellis Dee's Avatar
    Join Date
    Mar 2007
    Location
    New England
    Posts
    3,530

    Re: [RESOLVED] List drag&drop - help with resorting index of matching string

    brucevde, I agree with your general point that it's preferable to connect a display control (listview) with the data (array) using a single index value. In this case, though, it sounds to me like he's allowing the user to manually reorder the list using drag and drop functionality. I'm guessing he'd like save this order somehow, and so it seems reasonable to keep the list and array in sync.

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