Results 1 to 7 of 7

Thread: [RESOLVED] Move Item in the listbox

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Feb 2007
    Location
    Malaysia
    Posts
    1,370

    Resolved [RESOLVED] Move Item in the listbox

    How to move Item from the first index to second index one by one until last index. I have select the item. I want to move it down,just change the position. Code below does not works


    Code:
    For c = 0 To ListBox1.ListCount - 1
        
        If ListBox1.Selected(c) = True Then
                    Dim idx As Integer
                    idx = ListBox1.ListIndex
                    If ListBox1.ListIndex > 0 Then
                    ListBox1.ListIndex = idx - 1
                    
                    End If
                    
        End If
    Next
    Attached Images Attached Images  

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Move Item in the listbox

    Code:
    Private Sub cmdUp_Click()
        ' only if the first item isn't the current one
        If List1.ListIndex >= 0 Then
            ' add a duplicate item up in the listbox
            List1.AddItem List1.Text, List1.ListIndex - 1
            ' make it the current item
            List1.ListIndex = List1.ListIndex - 2
            ' delete the old occurrence of this item
            List1.RemoveItem List1.ListIndex + 2
        End If
    End Sub
    
    Private Sub cmdDown_Click()
        ' only if the last item isn't the current one
       If List1.ListIndex <> -1 And List1.ListIndex < List1.ListCount - 1 Then
            ' add a duplicate item down in the listbox
            List1.AddItem List1.Text, List1.ListIndex + 2
            ' make it the current item
            List1.ListIndex = List1.ListIndex + 2
            ' delete the old occurrence of this item
            List1.RemoveItem List1.ListIndex - 2
        End If
    End Sub

  3. #3
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: Move Item in the listbox

    Quote Originally Posted by Hack
    Code:
    Private Sub cmdUp_Click()
        ' only if the first item isn't the current one
        If List1.ListIndex >= 0 Then
            ' add a duplicate item up in the listbox
            List1.AddItem List1.Text, List1.ListIndex - 1
            ' make it the current item
            List1.ListIndex = List1.ListIndex - 2
            ' delete the old occurrence of this item
            List1.RemoveItem List1.ListIndex + 2
        End If
    End Sub
    Highlighted line should be:

    If List1.ListIndex > 0 Then


  4. #4
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: Move Item in the listbox

    Also, the following is basically the same but with few variables (some times it makes it easier for some people to understand):
    Code:
    Private Sub Command1_Click()
    Dim curItem$, curIndex%
    
        ' only if the first item isn't the current one
        If List1.ListIndex > 0 Then
            curIndex = List1.ListIndex
            curItem = List1.List(curIndex)
            List1.RemoveItem curIndex
            List1.AddItem curItem, curIndex - 1
            List1.Selected(curIndex - 1) = True
        End If
    
    End Sub

  5. #5

    Thread Starter
    Frenzied Member
    Join Date
    Feb 2007
    Location
    Malaysia
    Posts
    1,370

    Re: Move Item in the listbox

    Thank you Both of you... RhinoBull thank you for your correction.

  6. #6
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Move Item in the listbox

    Quote Originally Posted by RhinoBull
    Highlighted line should be:

    If List1.ListIndex > 0 Then

    I KNEW THAT!!!!!!!!!!!!!!!!!!

    I just wanted to see if you were paying attention.

    (slinks off in embarassment)

  7. #7

Posting Permissions

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



Click Here to Expand Forum to Full Width