|
-
Dec 18th, 2009, 10:22 AM
#1
Thread Starter
Hyperactive Member
[SOLVED] List1 Multiselect
Hi guys,
Im using this code to remove multiselected items :
For i = 0 To List1.ListCount
If List1.Selected(i) = true Then
List1.RemoveItem List1.ListIndex
End If
Next i
What i would like to to is to....remove all the non selected items.
I know it could be done by moving the selected ones to a new box and then back to the old one.
But is there a cleaner way? i tried the .selected(i)=false then .remove .List1.ListIndex but no luck
any ideas?
cheers
Last edited by batori; Dec 18th, 2009 at 11:52 AM.
Thanks for helping me out.
-
Dec 18th, 2009, 11:12 AM
#2
Member
Re: List1 Multiselect
I don't think the code you posted will work for either selected state. There are 3 problems:
1. The upper bound of the list is ListCount - 1 since the array of ListItems is zero-based.
2. You are removing the ListIndex item instead of the item at index i (your loop iterator).
3. If you remove items in a For loop controlled by the original ListCount, you will eventually reach an invalid index number.
Here's some code that will work. Notice that the loop is iterated backwards, starting with the highest index. This insures that the removal of an item doesn't affect the index of the next item being assessed.
Code:
Private Sub RemoveListItems(ByVal SelectedState As Boolean)
Dim i As Long
For i = List1.ListCount - 1 To 0 Step -1
If List1.Selected(i) = SelectedState Then
List1.RemoveItem i
End If
Next i
End Sub
-
Dec 18th, 2009, 11:51 AM
#3
Thread Starter
Hyperactive Member
Re: List1 Multiselect
thanks man...
that worked fine.
rating
Thanks for helping me out.
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
|