Link to home
Start Free TrialLog in
Avatar of d_jedi
d_jedi

asked on

Listbox drag and drop

Need to be able to drag and drop items to change their position in a listbox.

How can I do this (if it is possible)??
Avatar of AzraSound
AzraSound
Flag of United States of America image

if youre interested, this code will move items by double clicking them:

'MOVE ITEMS AROUND IN LISTBOX
'************************************************************************
'
'FORM CODE
'
'************************************************************************

Dim pos As Integer
Dim newpos As Integer
Dim strOld As String
Dim drag As Boolean
Dim i As Integer



Private Sub List1_DblClick()
    If drag = False Then
        drag = True
        pos = List1.ListIndex
        strOld = List1.List(pos)
        List1.List(pos) = "---" & List1.List(pos) & "---"
    Else
        newpos = List1.ListIndex
        If pos < newpos Then
            For i = pos To newpos
                List1.List(i) = List1.List(i + 1)
            Next
        ElseIf pos > newpos Then
            For i = pos To newpos Step -1
                List1.List(i) = List1.List(i - 1)
            Next
        Else 'clicked the same item twice
        End If
        List1.List(newpos) = strOld
        drag = False
    End If
   
End Sub

Private Sub List1_LostFocus()
    If drag = True Then
        List1.List(pos) = strOld
        drag = False
    End If
End Sub




Avatar of d_jedi
d_jedi

ASKER

This isn't what I was looking for..

I wanted to be able to drag and drop items in a list box to change their position in it..

Similar to how you can in Winamp or ICQ, if you've ever used those programs..
i know it isnt but as i know of no other solution i provided a simple workaround in case no one else can give you your answer.  i did some work and was unable to produce drag and drop within the same listbox.  sorry.
The listbox doesn’t provide feedback about where the mouse is.  If it did you could provide (still a crude) drag/drop functionality within itself.  Why don’t you consider using a ListView control  instead?  A ListView (in report view) looks exactly like a list box, but you have more control over all the elements of it, including dragging and dropping items within itself the way you describe (the way Internet Explorer does with the favorites list)

To make it work you need to:
 
Drop a ListView control on a form
Set the View property to 3 – lstReport
Be sure to create at least one column (use the Custom button to activate the property page)
Set the DragMode to 0 - vbManual
Also, set the DragIcon to an icon (otherwise you’d be dragging the outline of the control)

Add some stuff to the control for this example:
Private Sub Form_Load()

For x = 1 To 10
ListView1.ListItems.Add , , "hello"
Next x
ListView1.ListItems.Add , , "drag this baby"

End Sub


Create a public variable to hold what you are dragging:
Public DragItem As String


Start the drag in MouseDown (or other) event:

Set ListView1.DropHighlight = ListView1.HitTest(x, y)  ‘ This gets the item you want to move
DragItem = ListView1.DropHighlight.Text    ‘ and saves the text for dropping
ListView1.Drag 1  ‘ starts the dragging


Give the user feedback by highlighting which item it would be dropped near:
Private Sub ListView1_DragOver(Source As Control, x As Single, y As Single, State As Integer)
   Set ListView1.DropHighlight = ListView1.HitTest(x, y)
End Sub

Finally, drop the item:
Private Sub ListView1_DragDrop(Source As Control, x As Single, y As Single)
 ListView1.ListItems.Add ListView1.DropHighlight.Index, , DragItem
End Sub

I’ll leave it to you to provide code to delete the original position of the dragged item.

Hope this helps.

Rob

P.S. Instead of drag, you could use OleDrag… this would give your user the option to drop stuff in windows outside your program.
ASKER CERTIFIED SOLUTION
Avatar of Sage020999
Sage020999

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of d_jedi

ASKER

Please provide more information, Sage.

Which controls are you using on the form ??
Is this even for a listbox? (seems to me it's for a flexgrid?)
This particular control is the msFlexGrid.  Simular code has worked for me using the farpoint list boxes.  Which controls are you using. I will see if I have a project with drag and drop in them so that I can give you a cut and paste answer.
Avatar of d_jedi

ASKER

I'm just using a generic list box..
Avatar of d_jedi

ASKER

By the way, what's a farpoint listbox?
Far Point is a 3rd party control.
I don't think this can be done with a standard list box.  Can you use a differant control and make it appear to be a list box.  It would probally give you more flexability. If not, I would suggest in purchasing a 3rd party control that handles that task.
I don't think this can be done with a standard list box.  Can you use a differant control and make it appear to be a list box.  It would probally give you more flexability. If not, I would suggest in purchasing a 3rd party control that handles that task.
If this is the question:
> Need to be able to drag and drop items
> to change their position in a listbox.
> How can I do this (if it is possible)??


Then the answer is use the ListView control.  It will accommodate EXACTLY what you want.  The example with the FlexGrid has to do with moving columns.  How moving columns has anything to do with moving the items in the list I don’t know, especially since the ListBox doesn’t even support columns.

But you can move the items in a FlexGrid by dragging, as you can move the columns in a ListView by dragging.  But unless you need the greater functionality of the FlexGrid, you should just stick with the less imposing ListView.

Rob
I fiddled with the code to use drag and drop in the listbox.  Here is simple example:

Dim pos As Integer
Dim newpos As Integer
Dim strOld As String
Dim ListItems(20) As String

Private Sub Form_Load()
    Dim x As Integer
    For x = 0 To 20
        List1.AddItem "Item #" & x
    Next x
End Sub

Private Sub List1_MouseDown(Button As Integer, Shift As Integer, x As Single, Y As Single)
    pos = List1.ListIndex
    strOld = List1.List(pos)
    List1.MousePointer = 4
End Sub


Private Sub List1_MouseUp(Button As Integer, Shift As Integer, x As Single, Y As Single)
    Dim i As Integer
    Dim newpos As Integer
    Dim counter As Integer
    newpos = List1.ListIndex
    If pos < newpos Then
        For i = pos To newpos
            List1.List(i) = List1.List(i + 1)
        Next
    ElseIf pos > newpos Then
        For i = pos To newpos Step -1
            List1.List(i) = List1.List(i - 1)
        Next
    End If
    List1.List(newpos) = strOld
    List1.MousePointer = 0
End Sub