Moving items in listbox

Listbox with 10 numbered items in it.  VERY IMPORTANT - ITEMS ARE NUMBERED.

LISTBOX CONTENTS LOOK LIKE THIS:

1      Red
2      Green
3      Blue
4      Purple
5      ............

Code to rearrange items?  Move item 4 to item 3 - which would make item 3 - item 4.
My mind just can't think of an easy sub to do this. (Monday)

Also a sub to renumber the items if one is deleted from the middle.

Thanks.
mayfieldjrAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

j_a_deloozeCommented:
iterate thru the list box adding each item to an array, pass the array a function let the function do the work, resplit the array and assign to the list box appropriately
appariCommented:
try this

add the following sub

Private Sub SwapItems(first As Integer, Second As Integer)
Dim strTemp As String
strTemp = List1.List(Second)
List1.RemoveItem Second
List1.AddItem strTemp, first
End Sub

call it like this
SwapItems 3,4
Mike TomlinsonHigh School Computer Science, Computer Applications, and Mathematics TeachersCommented:
This will swap the position of the two indices passed to swapListBoxIndices(), but is the idea to swap the entire lines or leave the numbers in the same order and just swap the colors?

Private Sub Form_Load()
    List1.AddItem "1 Red"
    List1.AddItem "2 Green"
    List1.AddItem "3 Blue"
    List1.AddItem "4 Purple"
End Sub

Private Sub Command1_Click()
    swapListBoxIndices 0, 2
End Sub

Private Sub swapListBoxIndices(a As Integer, b As Integer)
    Dim tmp As String
    tmp = List1.List(a)
    List1.List(a) = List1.List(b)
    List1.List(b) = tmp
End Sub
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

pg_indiaCommented:
on what basic do you need to rearrange the listbox...

is it swapping only the numbers or name or what...
mayfieldjrAuthor Commented:
The numbers should stay in order but that is the least of my concern.  

So far the comment from idle mind is the best.  I came up with what J_A_Delooze suggested.

Yesterday I finnally got time to play with this and decided to use two command buttons.
"Move Up"  & "Move Down".  The Move Up one was easy but the code for that did not translate well to Move Down.  I did get something to work but it seems I should be able to use less code.

I work for a cellular phone service provider and will be using this to determine neighbor sites for CDMA technology.  Neighbor order is VERY important for CDMA.  That is why I have a number in front of the data.  When I change the data order I need to keep the numbers in order.  

I also add neighbors from another list box. I thought about drag & drop but a double click to move things from one list box to another was easier.

I really enjoy trying to figure these things out but once in a while suggestions from the good people at Experts Exchange is invaluable and brilliant.

Thank you all.  I will wait to see if any more good suggestions come in and then award the points.
Mike TomlinsonHigh School Computer Science, Computer Applications, and Mathematics TeachersCommented:
Making things move up and down in a listbox is deceivingly simple:

Private Sub Form_Load()
    List1.AddItem "1 Red"
    List1.AddItem "2 Green"
    List1.AddItem "3 Blue"
    List1.AddItem "4 Purple"
End Sub

Private Sub swapListBoxIndices(a As Integer, b As Integer)
    Dim tmp As String
    tmp = List1.List(a)
    List1.List(a) = List1.List(b)
    List1.List(b) = tmp
End Sub

Private Sub Up_Click()
    If List1.ListIndex >= 1 Then
        swapListBoxIndices List1.ListIndex, List1.ListIndex - 1
        List1.ListIndex = List1.ListIndex - 1
    End If
End Sub

Private Sub down_Click()
    If List1.ListIndex <> -1 And List1.ListIndex < List1.ListCount - 1 Then
        swapListBoxIndices List1.ListIndex, List1.ListIndex + 1
        List1.ListIndex = List1.ListIndex + 1
    End If
End Sub

Cheers

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Mike TomlinsonHigh School Computer Science, Computer Applications, and Mathematics TeachersCommented:
How are your listboxes coming along?
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.