Solved

Very Simple listbox question.

Posted on 2002-05-27
6
201 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
Independent Software Vendors: 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 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

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Hide vba in gp 7 114
VBA Shell can't Find Word document 11 127
MsgBox 4 71
VB script to continue despite error 2 48
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…
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…
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…

763 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