Results 1 to 5 of 5

Thread: Move checked items to top of listbox. (Resolved)

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Aug 2000
    Posts
    154

    Resolved Move checked items to top of listbox. (Resolved)

    Hey All,

    Let's say I have a listbox (style = 1 - checkbox) with the
    following code...

    Code:
    Private Sub Form_Load()
        List1.AddItem "apple", (0)
        List1.AddItem "orange", (1)
        List1.AddItem "banana", (2)
        List1.AddItem "grape", (3)
        List1.AddItem "lime", (4)
        List1.AddItem "lemon", (5)
    End Sub
    Now, let's say I select (check) three of the items in the listbox.
    Can someone please tell me how I can move those three items
    to the top of the list, when I click a button.

    I kind of figured that I will need to loop through the list, but I
    can't seem to get anything to work.

    Thanks in advance for any help,
    Ron
    Last edited by rdcody; Oct 5th, 2004 at 04:18 PM.

  2. #2
    Frenzied Member Jmacp's Avatar
    Join Date
    Jul 2003
    Location
    UK
    Posts
    1,959
    VB Code:
    1. Private Sub Command1_Click()
    2.  
    3. Dim i        As Long
    4. Dim myarr()  As String
    5. Dim nmyarr() As String
    6.  
    7.     ReDim myarr(List1.ListCount - 1) As String
    8.     ReDim nmyarr(List1.ListCount - 1) As String
    9.     With List1
    10.         For i = 0 To .ListCount - 1
    11.             If .Selected(i) Then
    12.                 myarr(i) = .List(i)
    13.             Else
    14.                 nmyarr(i) = .List(i)
    15.             End If
    16.         Next i
    17.     End With
    18.     List1.Clear
    19.     For i = LBound(myarr) To UBound(myarr)
    20.         If LenB(myarr(i)) = 0 Then
    21.            GoTo nextval
    22.         End If
    23.         List1.AddItem myarr(i)
    24.        
    25. nextval:
    26.     Next i
    27.     For i = LBound(nmyarr) To UBound(nmyarr)
    28.         If LenB(nmyarr(i)) = 0 Then
    29.            GoTo mnextval
    30.         End If
    31.         List1.AddItem nmyarr(i)
    32. mnextval:
    33.     Next i
    34.  
    35. End Sub

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Aug 2000
    Posts
    154
    Hey Jmacp,

    That works great except when they move to the top, they
    are unchecked. How can I keep them checked?

    Thanks,
    Ron

  4. #4
    Frenzied Member Jmacp's Avatar
    Join Date
    Jul 2003
    Location
    UK
    Posts
    1,959
    VB Code:
    1. Private Sub Command1_Click()
    2.  
    3. Dim i        As Long
    4. Dim myarr()  As String
    5. Dim nmyarr() As String
    6.  
    7.     ReDim myarr(List1.ListCount - 1) As String
    8.     ReDim nmyarr(List1.ListCount - 1) As String
    9.     With List1
    10.         For i = 0 To .ListCount - 1
    11.             If .Selected(i) Then
    12.                 myarr(i) = .List(i)
    13.             Else
    14.                 nmyarr(i) = .List(i)
    15.             End If
    16.         Next i
    17.     End With
    18.     List1.Clear
    19.     For i = LBound(myarr) To UBound(myarr)
    20.         If Not LenB(myarr(i)) = 0 Then
    21.           List1.AddItem myarr(i)
    22.           List1.Selected(j) = True
    23.           j = j + 1
    24.          
    25.         End If
    26.    
    27.     Next i
    28.     For i = LBound(nmyarr) To UBound(nmyarr)
    29.         If Not LenB(nmyarr(i)) = 0 Then
    30.           List1.AddItem nmyarr(i)
    31.          
    32.         End If
    33.    
    34.     Next i
    35.  
    36. End Sub

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Aug 2000
    Posts
    154
    Hey Jmacp,

    Oh yeah...that works great! I appreciate all your help.
    Have a good one!

    Thanks again,
    Ron

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