PDA

View Full Version : Solved: Reordering of listbox items.



mdmackillop
11-17-2006, 10:03 AM
I?ve created a userform to assist with inserting photos into a word document. Part of the process is to create a text file which contains the following information: Order, Path, Name, Description. I would like the facility to reorder the photos by dragging, or otherwise moving the descriptions within a userform listbox or similar
Sometimes only minor changes are required, other time there may be many. I don?t want to have to select each in turn to drag to another list.
The final result is to be a reordered text file containing the new order number against each Path/Name/Description.
Any thoughts?

Andy Pope
11-19-2006, 05:03 AM
I would use a couple of buttons to allow selected items to be moved up/down the list.
You can set the listbox to allow multiple selections.
Private Sub CommandButton2_Click()
'
' Move Items Up, allow multi selected items to move
'
Dim lngIndex As Long
Dim lngStartIndex As Long
Dim blnSelected() As Boolean

With ListBox1
ReDim blnSelected(.ListCount) As Boolean
For lngIndex = 0 To .ListCount - 1
blnSelected(lngIndex) = .Selected(lngIndex)
Next

lngStartIndex = -1
For lngIndex = 0 To .ListCount
If blnSelected(lngIndex) Then
If lngStartIndex = -1 Then lngStartIndex = lngIndex
Else
If lngStartIndex > 0 Then
SwapListboxItems ListBox1, lngStartIndex - 1, lngIndex - 1
lngStartIndex = -1
Else
lngStartIndex = -1
End If
End If
Next
End With

End Sub
Private Sub CommandButton3_Click()
'
' Move Items Down, allow multi selected items to move
'
Dim lngIndex As Long
Dim lngStartIndex As Long
Dim blnSelected() As Boolean

With ListBox1
ReDim blnSelected(.ListCount) As Boolean
For lngIndex = 0 To .ListCount - 1
blnSelected(lngIndex) = .Selected(lngIndex)
Next

lngStartIndex = -1
For lngIndex = 0 To .ListCount - 1
If blnSelected(lngIndex) Then
If lngStartIndex = -1 Then lngStartIndex = lngIndex
Else
If lngStartIndex >= 0 Then
SwapListboxItems ListBox1, lngIndex, lngStartIndex
lngStartIndex = -1
End If
End If
Next
End With

End Sub

Sub SwapListboxItems(Lst As MSForms.ListBox, FromIndex As Long, ToIndex As Long)
'
' Swap listbox items
'
ReDim strSubItemText(Lst.ColumnCount - 1) As String
Dim lngSubItemIndex As Long

For lngSubItemIndex = 0 To Lst.ColumnCount - 1
strSubItemText(lngSubItemIndex) = Lst.List(FromIndex, lngSubItemIndex)
Next

Lst.RemoveItem FromIndex
Lst.AddItem strSubItemText(0), ToIndex

For lngSubItemIndex = 1 To Lst.ColumnCount - 1
Lst.List(ToIndex, lngSubItemIndex) = strSubItemText(lngSubItemIndex)
Next

End Sub

Using drag and drop is possible but gets very messy very quickly when done within the same listbox. Also scrolling whilst dragging is not automatic so trying to drag the top item to the bottom on a list that requires scrolling doesn't work.

mdmackillop
11-19-2006, 05:44 AM
Thanks Andy,
Just what I was looking for. To simplify presentation, I renamed your button codes and added a SpinButton as follows
Regards
Malcolm

Private Sub SpinButton1_Change()
If SpinButton1 = 1 Then MoveUp
If SpinButton1 = -1 Then MoveDown
SpinButton1.Value = 0
End Sub

mdmackillop
11-19-2006, 08:29 AM
Final version.

lucas
11-19-2006, 10:22 AM
I'm probably missing something Malcolm...please let me know if I misunderstand but it doesn't save the changes to the order to the text file for me unless I comment out this line:
BubbleSort Data
in cmdReWrite_Click

This is a good idea though..I can see where it would be useful

mdmackillop
11-19-2006, 10:33 AM
Hi Steve,
There is no "save" procedure. The process is to:
Change the order number stored in the first three characters of each line listed in the array
Sort the array using BubbleSort
Kill the old file
Create a new file base on the reordered array.

Let me know if you find any problem.

lucas
11-19-2006, 10:44 AM
I see that but its supposed to create the new file with the same path and filename as the opened file..? I just expected the changes in order to be reflected in the textfile after the operation.

mdmackillop
11-19-2006, 10:49 AM
The textfile is closed after each step of reading/writing (a.close), When ReWrite is run, the text file is rewritten then reopened to populate the listbox.

lucas
11-19-2006, 10:57 AM
I follow it now....thanks Malcolm

Ken Puls
01-10-2017, 09:24 PM
Heya Strangers!


I would use a couple of buttons to allow selected items to be moved up/down the list.

Just wanted to pop a quick note to say how awesome this code is, Andy. I needed to do the same thing here, copied, pasted, updated my form names and it just worked. Beautiful!

Hope you guys are all well these days. :)