VB6 Snippet - Move Items In A Listbox-VBForums
Results 1 to 6 of 6

Thread: VB6 Snippet - Move Items In A Listbox

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2005
    Location
    South Africa
    Posts
    760

    VB6 Snippet - Move Items In A Listbox

    I've seen lots of people asking for a way to do this so here is an extremely simple function to rearrange the items in a listbox:
    VB Code:
    1. Private Function MoveListboxItem(List1 As ListBox, CurrentIndex As Integer, NewIndex As Integer)
    2. Dim strItem As String
    3. strItem = List1.List(CurrentIndex)
    4. List1.RemoveItem CurrentIndex
    5. List1.AddItem strItem, NewIndex
    6. End Function
    If I helped you out, please consider adding to my reputation!

    -- "The faulty interface lies between the chair and the keyboard" --

    VB6 Programs By Me:
    ** Dictionary, Thesaurus & Rhyme-Generator In One ** WMP Recent Files List Editor ** Pretty Impressive Clock ** Extract Firefox History **

  2. #2
    Lively Member ixtrip's Avatar
    Join Date
    Aug 2005
    Posts
    64

    Re: VB6 Snippet - Move Items In A Listbox

    Here's the code so you wont get an error if you have it set to be using a command button to move an item in the listbox and someone tries to move it higher or lower then it's supposed to go.


    VB Code:
    1. Private Function MoveListboxItem(List1 As ListBox, CurrentIndex As Integer, NewIndex As Integer)
    2. Dim strItem As String
    3. If List1.ListCount = NewIndex Or NewIndex = -1 Then Exit Function
    4. strItem = List1.List(CurrentIndex)
    5. List1.RemoveItem CurrentIndex
    6. List1.AddItem strItem, NewIndex
    7. List1.ListIndex = NewIndex
    8. End Function

    This one here i changed so basically you just set it as True if you want to move the listitem up, if you want it to go down then set it to false

    VB Code:
    1. Private Function MoveListboxItem(List1 As ListBox, CurrentIndex As Integer, Up As Boolean)
    2. Dim strItem As String
    3. Dim NewIndex As Integer
    4.     If Up = True Then
    5.     NewIndex = CurrentIndex - 1
    6.     Else
    7.     NewIndex = CurrentIndex + 1
    8.     End If
    9. If List1.ListCount = NewIndex Or NewIndex < 0 Then Exit Function
    10. strItem = List1.List(CurrentIndex)
    11. List1.RemoveItem CurrentIndex
    12. List1.AddItem strItem, NewIndex
    13. List1.ListIndex = NewIndex
    14. End Function
    Last edited by ixtrip; Sep 6th, 2005 at 08:51 PM.

  3. #3
    Super Moderator Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,335

    Re: VB6 Snippet - Move Items In A Listbox

    More tricks for moving items around in a Listbox.
    VB Code:
    1. Private Sub cmdUp_Click()
    2.     ' only if the first item isn't the current one
    3.     If List1.ListIndex >= 0 Then
    4.         ' add a duplicate item up in the listbox
    5.         List1.AddItem List1.Text, List1.ListIndex - 1
    6.         ' make it the current item
    7.         List1.ListIndex = List1.ListIndex - 2
    8.         ' delete the old occurrence of this item
    9.         List1.RemoveItem List1.ListIndex + 2
    10.     End If
    11. End Sub
    12.  
    13. Private Sub cmdDown_Click()
    14.     ' only if the last item isn't the current one
    15.    If List1.ListIndex <> -1 And List1.ListIndex < List1.ListCount - 1 Then
    16.         ' add a duplicate item down in the listbox
    17.         List1.AddItem List1.Text, List1.ListIndex + 2
    18.         ' make it the current item
    19.         List1.ListIndex = List1.ListIndex + 2
    20.         ' delete the old occurrence of this item
    21.         List1.RemoveItem List1.ListIndex - 2
    22.     End If
    23. End Sub
    24.  
    25. Private Sub List1_Click()
    26.     ' enable/disable buttons when the current item changes
    27.     cmdUp.Enabled = (List1.ListIndex > 0)
    28.     cmdDown.Enabled = (List1.ListIndex <> -1 And List1.ListIndex <  List1.ListCount - 1)
    29. End Sub
    30. 'If you want to enable and disable the two buttons as soon as you show the form,
    31. 'remember to set the ListBox's ListIndex property explicitly in the Form_Load event
    32. 'to a value other than -1, or manually call the List1_Click procedure:
    33. Private Sub Form_Load()
    34.     ' because there is no current item (ListIndex = -1) you
    35.     ' must call the Click procedure manually
    36.     Call List1_Click
    37. End Sub
    Please use [Code]your code goes in here[/Code] tags when posting code.
    When you have received an answer to your question, please mark it as resolved using the Thread Tools menu.
    Before posting your question, did you look here?
    Got a question on Linux? Visit our Linux sister site.
    I dont answer coding questions via PM or EMail. Please post a thread in the appropriate forum section.

    Creating A Wizard In VB.NET
    Paging A Recordset
    What is wrong with using On Error Resume Next
    Good Article: Language Enhancements In Visual Basic 2010
    Upgrading VB6 Code To VB.NET
    Microsoft MVP 2005/2006/2007/2008/2009/2010/2011/2012/Defrocked

  4. #4
    Addicted Member
    Join Date
    Jul 2002
    Location
    Toronto, ON Canada
    Posts
    153

    Re: VB6 Snippet - Move Items In A Listbox

    Thanks! Saved me from having to ask my Q!

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2005
    Location
    South Africa
    Posts
    760

    Re: VB6 Snippet - Move Items In A Listbox

    There you have it! Solutions ranging from v. simple to v. useful!
    If I helped you out, please consider adding to my reputation!

    -- "The faulty interface lies between the chair and the keyboard" --

    VB6 Programs By Me:
    ** Dictionary, Thesaurus & Rhyme-Generator In One ** WMP Recent Files List Editor ** Pretty Impressive Clock ** Extract Firefox History **

  6. #6
    Fanatic Member coolcurrent4u's Avatar
    Join Date
    Apr 2008
    Location
    *****
    Posts
    970

    Re: VB6 Snippet - Move Items In A Listbox

    Hack, your code seems to have bug, so here is my modification
    vb Code:
    1. Private Sub cmdUp_Click()
    2.     Dim currIndex As Integer
    3.     Dim currItem  As String
    4.     Dim bSel As Boolean
    5.  
    6.     If lbItems.ListIndex >= 1 Then
    7.         currIndex = lbItems.ListIndex
    8.         currItem = lbItems.List(currIndex)
    9.         bSel = lbItems.Selected(currIndex)
    10.         lbItems.RemoveItem currIndex
    11.         lbItems.AddItem currItem, currIndex - 1
    12.         lbItems.ListIndex = currIndex - 1
    13.         lbItems.Selected(currIndex - 1) = bSel
    14.     End If
    15. End Sub
    16.      
    17. Private Sub cmdDown_Click()
    18.     Dim currIndex As Integer
    19.     Dim currItem  As String
    20.     Dim bSel As Boolean
    21.    
    22.     If lbItems.ListIndex <> -1 And lbItems.ListIndex < lbItems.ListCount - 1 Then
    23.         currIndex = lbItems.ListIndex
    24.         currItem = lbItems.List(currIndex)
    25.         bSel = lbItems.Selected(currIndex)
    26.         lbItems.RemoveItem currIndex
    27.         lbItems.AddItem currItem, currIndex + 1
    28.         lbItems.ListIndex = currIndex + 1
    29.         lbItems.Selected(currIndex + 1) = bSel
    30.     End If
    31. End Sub
    Last edited by coolcurrent4u; Oct 15th, 2012 at 08:22 PM.
    Programming is all about good logic. Spend more time here


    (vHost for Apache) (Generate pronounceable password) (Generate random number c#) (Filter array with another array)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.