Solved

Moving items in listbox

Posted on 2003-10-27
7
1,118 Views
Last Modified: 2013-12-26
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.
0
Comment
Question by:mayfieldjr
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 

Expert Comment

by:j_a_delooze
ID: 9630232
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
0
 
LVL 39

Expert Comment

by:appari
ID: 9630362
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
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 9630908
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
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 3

Expert Comment

by:pg_india
ID: 9632496
on what basic do you need to rearrange the listbox...

is it swapping only the numbers or name or what...
0
 

Author Comment

by:mayfieldjr
ID: 9634097
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.
0
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 300 total points
ID: 9635574
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
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 9688120
How are your listboxes coming along?
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses
Course of the Month7 days, 7 hours left to enroll

622 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question