|
-
Oct 5th, 2004, 12:23 PM
#1
Thread Starter
Addicted Member
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.
-
Oct 5th, 2004, 12:44 PM
#2
VB Code:
Private Sub Command1_Click()
Dim i As Long
Dim myarr() As String
Dim nmyarr() As String
ReDim myarr(List1.ListCount - 1) As String
ReDim nmyarr(List1.ListCount - 1) As String
With List1
For i = 0 To .ListCount - 1
If .Selected(i) Then
myarr(i) = .List(i)
Else
nmyarr(i) = .List(i)
End If
Next i
End With
List1.Clear
For i = LBound(myarr) To UBound(myarr)
If LenB(myarr(i)) = 0 Then
GoTo nextval
End If
List1.AddItem myarr(i)
nextval:
Next i
For i = LBound(nmyarr) To UBound(nmyarr)
If LenB(nmyarr(i)) = 0 Then
GoTo mnextval
End If
List1.AddItem nmyarr(i)
mnextval:
Next i
End Sub
-
Oct 5th, 2004, 01:20 PM
#3
Thread Starter
Addicted Member
Hey Jmacp,
That works great except when they move to the top, they
are unchecked. How can I keep them checked?
Thanks,
Ron
-
Oct 5th, 2004, 01:51 PM
#4
VB Code:
Private Sub Command1_Click()
Dim i As Long
Dim myarr() As String
Dim nmyarr() As String
ReDim myarr(List1.ListCount - 1) As String
ReDim nmyarr(List1.ListCount - 1) As String
With List1
For i = 0 To .ListCount - 1
If .Selected(i) Then
myarr(i) = .List(i)
Else
nmyarr(i) = .List(i)
End If
Next i
End With
List1.Clear
For i = LBound(myarr) To UBound(myarr)
If Not LenB(myarr(i)) = 0 Then
List1.AddItem myarr(i)
List1.Selected(j) = True
j = j + 1
End If
Next i
For i = LBound(nmyarr) To UBound(nmyarr)
If Not LenB(nmyarr(i)) = 0 Then
List1.AddItem nmyarr(i)
End If
Next i
End Sub
-
Oct 5th, 2004, 02:07 PM
#5
Thread Starter
Addicted Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|