Results 1 to 3 of 3

Thread: [Resolved] Move item inside listbox (up and down)

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2001
    Location
    www.alexdata.com
    Posts
    484

    Exclamation [Resolved] Move item inside listbox (up and down)

    Hey, anyone with a simple code for moving items up or down in a lsitbox?
    I mean if you have the items 1 - 5 in a listbox,and you would like
    the item5 to become item2, then you simply click item5, hold button and drop where item2 is and then we get a list that looks like this:

    NEW: ----- ORGINAL:
    item1 ------ item1
    item5 ------ item2
    item2 ------ item3
    item3 ------ item4
    item4 ------ item5

    Do you understand what i want? lets say its equal to winamp's behavior, for example...

    Please help! PS: keep the code as simple as possible!
    Last edited by alexdata; Mar 6th, 2003 at 08:40 AM.
    ***************
    Please use [highlight=vb] ..your code.. [/highlight] when posting code!

    When you have received the working answer to your question,
    please mark it as *SOLVED* + Your Questions Title ...using your Thread's Tool menu.


    Also try to point out what answer made it work for you, or edit your first post to contain a quote of the correct answer...

    Please Answer All Questions With Working Code Examples...


    My Unfinished Projects and My working Programs
    ***************

  2. #2
    Frenzied Member
    Join Date
    Jan 2000
    Location
    Bellevue, WA, USA
    Posts
    1,357

    Here's one way to do it...

    Could use a little polishing, but this should get you started.
    • First, set the OLEDragMode property of the ListBox to Automatic
    • Second, set the OLEDopMode of the ListBox to Manual
    This will allow you to capture drag and drop events in the list box.

    Then, you should create some global variables to capture what the user is doing and what data they are playing with. I created the following:
    • Variable to track the text of the last item selected in the list box
    • Variable to track the index of the last item selected in the list box
    • Variable to track if the user is dragging from our list box (as opposed to some other source)
    Then, I added code into the OLEDragDrop event to determine where in the list box the user was dropping, then I removed the index they originally selected and added the text at the index where they were dropping.

    Here's the code, hope it helps:
    VB Code:
    1. Option Explicit
    2.  
    3. ' Module level variables hold information about dragging stuff in the list box
    4. Private DragData As String
    5. Private DragFromLocation As Integer
    6. Private IsDragging As Boolean
    7.  
    8. Private Sub Form_Load()
    9.     ' Load some fake data for testing
    10.     List1.Clear
    11.     List1.AddItem "one"
    12.     List1.AddItem "two"
    13.     List1.AddItem "three"
    14.     List1.AddItem "four"
    15.     List1.AddItem "five"
    16. End Sub
    17.  
    18. Private Sub List1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
    19.     If Button = vbLeftButton Then
    20.         ' Always save the text and index of the last item clicked on because it may be dragged
    21.         DragData = List1.List(List1.ListIndex)
    22.         DragFromLocation = List1.ListIndex
    23.     End If
    24. End Sub
    25.  
    26. Private Sub List1_OLECompleteDrag(Effect As Long)
    27.     ' User is done dragging an item from the list box, so set our flag
    28.     IsDragging = False
    29. End Sub
    30.  
    31. Private Sub List1_OLEDragDrop(Data As DataObject, Effect As Long, Button As Integer, _
    32.                               Shift As Integer, X As Single, Y As Single)
    33.     Dim DropLocation As Integer
    34.    
    35.     ' Make sure we are the source for the drag
    36.     If IsDragging Then
    37.         ' Figure out how many items down in the list (the index) where we need to add this item
    38.         DropLocation = Int(Y / Me.TextHeight("Text"))
    39.         If DropLocation < 0 Then DropLocation = 0
    40.         If DropLocation >= List1.ListCount Then DropLocation = List1.ListCount - 1
    41.        
    42.         ' Remove the dragged item
    43.         List1.RemoveItem DragFromLocation
    44.        
    45.         ' And insert it in the new position
    46.         List1.AddItem DragData, DropLocation
    47.        
    48.         ' And we are no longer dragging...
    49.         IsDragging = False
    50.     End If
    51. End Sub
    52.  
    53. Private Sub List1_OLEStartDrag(Data As DataObject, AllowedEffects As Long)
    54.     ' User is dragging an item from the list box, so set our flag
    55.     IsDragging = True
    56. End Sub
    Last edited by seaweed; Mar 4th, 2003 at 06:00 PM.
    ~seaweed

  3. #3
    Hyperactive Member
    Join Date
    May 2002
    Posts
    434
    Hello Seaweed

    I tried your code and it works well except for the fact that it will not drag an item till it is highlighted. Example:
    I click on an item in the listbox and attempt to drag it to a new loacation. But I just end up dragging the highlight to the new location. There is no drag icon.

    But if I single click on the listitem and highlight it. Once it is highlighted then I can click on it and drag it.

    I can't figure out why the OLEStartDrag event will not fire till the item is highlighted.

    Any Ideas

    Thanks

    David

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