Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

It seems like it ought to be simple

Status
Not open for further replies.

JonJacobik

Technical User
Dec 9, 2001
27
US
I have listbox with several hundred items. I'd like the user to be able to rearrange the items using drag and drop.I'm missing something. It seems that I should be able to grasp this. The drag operation get's me started, but I can't figure out where I'm dropping the object.

Thanks in advance.

Jon
 
I don't believe that VB supports this natevly. It may be possible through the API. The drag mtehod/properties refer to draging the entire control. It is possible to drag one list box onto another one and insert the select item but thats as close as I can come to what your looking for without using the API.
 
In case your interested here's a sample of dragging to another list box. It's kind of clumsy looking and I woudn't use it but it might help you understand how the drag commands work. This assumes you have two listbox (list1, list2) on a form.

Code:
Option Explicit
Private isDragging As Boolean

Private Sub Form_Load()
    Dim i As Integer
    For i = 1 To 10
        List1.AddItem Format$(i)
    Next
End Sub

Private Sub List1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
    If Button = 1 Then
        List1.Drag vbBeginDrag
        isDragging = True
    End If
End Sub


Private Sub List1_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
    If Button = 1 And isDragging Then
        List1.Drag vbEndDrag
        isDragging = False
    End If
End Sub

Private Sub List2_DragDrop(Source As Control, X As Single, Y As Single)
    List2.AddItem Source.Text
    Source.RemoveItem Source.ListIndex
End Sub
 
Thanks much. At least I'm confident that it can't be done, rather than something I just couldn't figure out. Now if I can just figure out how my comptitor is doing it.

 
I haven't done it (don't have time at present) but it's always possible.

Might be worth writing your own in VB, maybe based on an array of labels or textboxes.
You could use Mousedown to select entire text, then use DragDrop to swap text around Let me know if this helps
________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first

'There are 10 kinds of people in the world: those who understand binary, and those who don't.'
 
You don't use D&D here, use mousedown and mouseup events.

Here's a sample of the general idea:

Option Explicit
Dim Downpntr As Integer
Dim Downstring As String
Dim UpString As String

Private Sub List1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)

Downpntr = List1.ListIndex
Downstring = List1.List(Downpntr)

End Sub

Private Sub List1_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)

' want to swap these two
UpString = List1.List(List1.ListIndex)
List1.List(List1.ListIndex) = Downstring
List1.List(Downpntr) = UpString

End Sub


User left clicks and holds on the list, then drags to the new position, then lets go of the mouse. List will scroll automatically as you move the mouse above or below it.

Hope this helps,

Robert
 
Of course, this just swaps the two items. If you want to insert the item you dropped without displacing the old item, you will need to write code to do that. ( probably using a dynamic array and refreshing the list box with the new data ). But you get the general idea.

Robert
 
Try this for the "insert" method. Don't need an array after all....

Option Explicit
Dim Downpntr As Integer
Dim Downstring As String

Private Sub List1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)

Downpntr = List1.ListIndex
Downstring = List1.List(Downpntr)

End Sub

Private Sub List1_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
Dim X2 As Integer

' this places the "dragged" item just below the item that was highlighted when you released the mouse button.
' It then moves all of the other items up or down as necessary.

If Downpntr = List1.ListIndex Then Exit Sub

If Downpntr < List1.ListIndex Then
For X2 = (Downpntr + 1) To List1.ListIndex
List1.List(X2 - 1) = List1.List(X2)
Next X2
List1.List(X2 - 1) = Downstring
Else
For X2 = Downpntr To (List1.ListIndex + 2) Step -1
List1.List(X2) = List1.List(X2 - 1)
Next X2
List1.List(List1.ListIndex + 1) = Downstring
End If


End Sub


Robert
 
Vampire - Robert Thanks a million. Code works. It's fast,quick and easy and it works!

Jon
 
I suppose you could. You would just have to handle everything in a group of 3. You'd need to change the &quot;DownString&quot; variable to an array, to save those 3 items in that group.

You would figure the group you clicked on by doing:

Downpntr = Int(List1.ListIndex / 3) * 3

Then it would just be a matter of shifting everything up or down depending on the group.

Robert
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top