Results 1 to 7 of 7

Thread: Drag and drop inside of the list box

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 1999
    Posts
    305

    Post

    I have a list box with a bunch of things, but there are so many that one must scroll, and I want the most important ones towards the top. How do I let the user drag an item in the list box and drop it anywhere on the same list box? Also, how could I get it to manually drag a corresponding item in another listbox so the two are always in the same pairing? Any help would be great.

    Thanks in advance,

    bob

  2. #2
    New Member
    Join Date
    Jan 2000
    Posts
    10

    Post

    Well just as a matter of easing your program interface, you could add a textbox above the listbox, where the user can begin to type in letters an it scrolls down to anything resorting to what they typed.

    ------------------
    - M e n S h e n
    Canabatech Programmer

    Homepage http://influx.virtualave.net
    Email [email protected]
    ICQ 17527422



  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 1999
    Posts
    305

    Post

    but I want the user to be able to rearrange the elements in the box. How would I do that?

  4. #4
    Serge's Avatar
    Join Date
    Feb 1999
    Location
    Scottsdale, Arizona, USA
    Posts
    2,744

    Post

    Sure thing.

    Code:
    Private Type POINTAPI
       X As Long
       Y As Long
    End Type
    Private Declare Function LBItemFromPt Lib "comctl32.dll" (ByVal hwnd As Long, ByVal ptx As Long, ByVal pty As Long, ByVal bAutoScroll As Long) As Long
    Private Declare Function ClientToScreen Lib "user32" (ByVal hwnd As Long, lpPoint As POINTAPI) As Long
    
    Private Sub List1_DragDrop(Source As Control, X As Single, Y As Single)
        Dim intCurItem As Long
        Dim pt As POINTAPI
        Dim strItem As String
        
        
        'adjust to the expected values
        pt.X = X \ Screen.TwipsPerPixelX
        pt.Y = Y \ Screen.TwipsPerPixelY     'and map to the client coordinates
        Call ClientToScreen(List1.hwnd, pt)     'get the item nearest the cursor
        intCurItem = LBItemFromPt(List1.hwnd, pt.X, pt.Y, False)
        If intCurItem > -1 Then
            strItem = List1.List(List1.ListIndex)
            List1.RemoveItem List1.ListIndex
            List1.AddItem strItem, intCurItem
        End If
    End Sub
    
    Private Sub List1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
        If Button = vbLeftButton Then
            List1.Drag vbBeginDrag
        End If
    End Sub
    Make sure you set the DragIcon property to the apropriate cursor.

    ------------------

    Serge

    Programmer Analyst
    [email protected]
    [email protected]
    ICQ#: 51055819


  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 1999
    Posts
    305

    Post

    Wow Serge! this is great! thank you very much. Just one more thing. After I drag it, I want another listbox to do the same thing. For example, if I had:

    1 1
    2 2
    3 3
    4 4

    and I switched two from the first listbox:

    1 1
    4 2
    3 3
    2 4

    how do I get the second listbox to have the same order as the first:

    1 1
    4 4
    3 3
    2 2

    (the two lists are paired sets of data, not the same data repeated twice.) I tried adding the following right after the List1.AddItem, but it didn't work, and I don't understand why.

    List2.RemoveItem List1.ListIndex
    List2.AddItem strItem, intCurItem

    Thanks again for your help.

  6. #6
    Serge's Avatar
    Join Date
    Feb 1999
    Location
    Scottsdale, Arizona, USA
    Posts
    2,744

    Post

    Actually, what you wrote should do the job if the lists are equal:

    Code:
    Private Type POINTAPI
       X As Long
       Y As Long
    End Type
    Private Declare Function LBItemFromPt Lib "comctl32.dll" (ByVal hwnd As Long, ByVal ptx As Long, ByVal pty As Long, ByVal bAutoScroll As Long) As Long
    Private Declare Function ClientToScreen Lib "user32" (ByVal hwnd As Long, lpPoint As POINTAPI) As Long
    
    Private Sub List1_DragDrop(Source As Control, X As Single, Y As Single)
        Dim intCurItem As Long
        Dim pt As POINTAPI
        Dim strItem As String
        
        
        'adjust to the expected values
        pt.X = X \ Screen.TwipsPerPixelX
        pt.Y = Y \ Screen.TwipsPerPixelY     'and map to the client coordinates
        Call ClientToScreen(List1.hwnd, pt)     'get the item nearest the cursor
        intCurItem = LBItemFromPt(List1.hwnd, pt.X, pt.Y, False)
        If intCurItem > -1 Then
            strItem = List1.List(List1.ListIndex)
            List1.RemoveItem List1.ListIndex
            List2.RemoveItem List1.ListIndex
            List1.AddItem strItem, intCurItem
            List2.AddItem strItem, intCurItem
        End If
    End Sub
    
    Private Sub List1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
        If Button = vbLeftButton Then
            List1.Drag vbBeginDrag
        End If
    End Sub
    ------------------

    Serge

    Programmer Analyst
    [email protected]
    [email protected]
    ICQ#: 51055819


  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 1999
    Posts
    305

    Post

    I got it to work, but it was all strange because there were actually 3 listboxes on two different forms that had to be updated but it works now thanks to you, Serge.

    Thanks

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