Solved

Very Simple listbox question.

Posted on 2002-05-27
6
199 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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

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…
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…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

776 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