Solved

Very Simple listbox question.

Posted on 2002-05-27
6
203 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
[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
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
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

 
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

Enroll in June's Course of the Month

June’s Course of the Month is now available! Experts Exchange’s Premium Members, Team Accounts, and Qualified Experts have access to a complimentary course each month as part of their membership—an extra way to sharpen your skills and increase training.

Question has a verified solution.

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

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

707 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