Solved

Very Simple listbox question.

Posted on 2002-05-27
6
196 Views
Last Modified: 2012-05-04
Experts,

All I wish to do is with the use of 2 command buttons move a selected item in a listbox up and down in position depending on which command button is pressed..

Madmarlin
0
Comment
Question by:Madmarlin
6 Comments
 
LVL 43

Expert Comment

by:TimCottee
ID: 7037184
Ok, this is a little tricky but not too difficult:

Private Sub cmdDown_Click()
    Dim strItem As String
    Dim intData As Integer
    Dim intIndex As Integer
    intIndex = List1.ListIndex
    If intIndex = 0 Then
        Exit Sub
        'as we are at the top
    End If
    strItem = List1.List(intIndex)
    intData = List1.ItemData(intIndex)
    List1.RemoveItem intIndex
    List1.AddItem strItem, intIndex + 1
    List1.ItemData(intIndex + 1) = intData
    List1.ListIndex = List1.NewIndex
End Sub

Private Sub cmdUp_Click()
    Dim strItem As String
    Dim intData As Integer
    Dim intIndex As Integer
    intIndex = List1.ListIndex
    If intIndex = List1.ListCount - 1 Then
        Exit Sub
        'as we are at the bottom
    End If
    strItem = List1.List(intIndex)
    intData = List1.ItemData(intIndex)
    List1.RemoveItem intIndex
    List1.AddItem strItem, intIndex - 1
    List1.ItemData(intIndex - 1) = intData
    List1.ListIndex = List1.NewIndex
End Sub
0
 
LVL 43

Accepted Solution

by:
TimCottee earned 70 total points
ID: 7037216
Looks like I got the test for top and bottom the wrong way round, here is a better attempt;

Private Sub cmdDown_Click()
    Dim strItem As String
    Dim intData As Integer
    Dim intIndex As Integer
    intIndex = List1.ListIndex
    If intIndex = List1.ListCount - 1 Then
        Exit Sub
        'as we are at the top
    End If
    strItem = List1.List(intIndex)
    intData = List1.ItemData(intIndex)
    List1.RemoveItem intIndex
    List1.AddItem strItem, intIndex + 1
    List1.ItemData(intIndex + 1) = intData
    List1.ListIndex = List1.NewIndex
End Sub

Private Sub cmdUp_Click()
    Dim strItem As String
    Dim intData As Integer
    Dim intIndex As Integer
    intIndex = List1.ListIndex
    If intIndex = 0 Then
        Exit Sub
        'as we are at the bottom
    End If
    strItem = List1.List(intIndex)
    intData = List1.ItemData(intIndex)
    List1.RemoveItem intIndex
    List1.AddItem strItem, intIndex - 1
    List1.ItemData(intIndex - 1) = intData
    List1.ListIndex = List1.NewIndex
End Sub
0
 
LVL 25

Expert Comment

by:Mr_Peerapol
ID: 7037484
Private Sub cmdUp_Click()
  If List1.ListCount=0 Or List1.ListIndex=0 Then Exit Sub
  List1.ListIndex = List1.ListIndex - 1
End Sub

Private Sub cmdDown_Click()
  If List1.ListCount=0 or List1.ListIndex = List1.ListCount-1 Then Exit Sub
  List1.ListIndex = List1.ListIndex + 1
End If
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 1

Expert Comment

by:killer5
ID: 7038451
Private Sub cmdUp_Click()
If List1.ListCount=0 Or _
List1.ListIndex=0 Then Exit Sub

Dim tStr as string
Dim tIndex as integer
tStr = List1.List(List1.ListIndex)
tIndex = List1.ListIndex
List1.RemoveItem tIndex
List1.AddItem tStr,tIndex - 1
List1.ListIndex = tIndex - 1
End Sub

Private Sub cmdDown_Click()
If List1.ListCount=0 Or _
List1.ListIndex=List1.ListCount-1 Then Exit Sub

Dim tStr as string
Dim tIndex as integer
tStr = List1.List(List1.ListIndex)
tIndex = List1.ListIndex
List1.RemoveItem tIndex
List1.AddItem tStr,tIndex + 1
List1.Listindex = tIndex + 1
End Sub
0
 
LVL 1

Author Comment

by:Madmarlin
ID: 7038878
Once again Tim you win...

As this is the 3rd or 4th question of mine you've answer you win an addition 20 points..

Thanks Madmarlin
0
 
LVL 43

Expert Comment

by:TimCottee
ID: 7038912
Thanks to you Madmarlin, glad to have helped.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
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…

747 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now