Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Very Simple listbox question.

Posted on 2002-05-27
6
Medium Priority
?
222 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 280 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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…
Suggested Courses

886 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