Newbie q: Remove selected items from multiselect listbox? 
Author Message
 Newbie q: Remove selected items from multiselect listbox?

What is the best solution for removing selected items (I used Multiselect=1
<simple>) from a listbox?

I *know* this doesn't work:

For t = 1 To List1.ListCount()
  If List1.Selected(t - 1) Then
    List1.RemoveItem (t - 1)
  End If
Next t

because by the time the counter reaches List1.Listcount, some items are
already deleted. But what is the "elegant" solution.

Thanks in advance,

Richard Douwes



Thu, 07 Aug 2003 04:30:29 GMT  
 Newbie q: Remove selected items from multiselect listbox?
Your approach would work if you use a ListView (more elegant) where you can use
the ListView's
ListItems collection.  Otherwise, this example uses two Collections with a
ListBox.

Option Explicit
Dim Selected As New Collection
Dim Unselected As New Collection
==========================
Private Sub Command1_Click()
Dim i As Long
Dim t As Long
Dim j As Variant

    t = List1.ListCount
    If t = 0 Then Exit Sub
    'Loop List1 and add each item to the appropriate Collection
    For i = 0 To t - 1
        If List1.Selected(i) Then
            Selected.Add List1.List(i)
        Else
            Unselected.Add List1.List(i)
        End If
    Next

    List1.Clear

    'Repopulate List1 with the items in Unselected
    For Each j In Unselected
        List1.AddItem j
    Next

    'Likewise, you can do something with the selected items
    'For Each j In Selected
        '(Do some process)
    'Next

    Set Selected = Nothing
    Set Unselected = Nothing

End Sub

Rob

Quote:

> What is the best solution for removing selected items (I used Multiselect=1
> <simple>) from a listbox?

> I *know* this doesn't work:

> For t = 1 To List1.ListCount()
>   If List1.Selected(t - 1) Then
>     List1.RemoveItem (t - 1)
>   End If
> Next t

> because by the time the counter reaches List1.Listcount, some items are
> already deleted. But what is the "elegant" solution.

> Thanks in advance,

> Richard Douwes



Thu, 07 Aug 2003 07:07:59 GMT  
 Newbie q: Remove selected items from multiselect listbox?
Do it in reverse .. this way t will always be = or < than count.

For t = List1.ListCount -1 To 0
  If List1.Selected(t) Then
    List1.RemoveItem (t)
  End If
Next t

--

Randy Birch
MVP Visual Basic

Take the vb.net poll at:
http://www.mvps.org/vbnet/
http://www.mvps.org/ccrp/

Please respond only to the newsgroups so all can benefit.


: What is the best solution for removing selected items (I used
Multiselect=1
: <simple>) from a listbox?
:
: I *know* this doesn't work:
:
: For t = 1 To List1.ListCount()
:   If List1.Selected(t - 1) Then
:     List1.RemoveItem (t - 1)
:   End If
: Next t
:
: because by the time the counter reaches List1.Listcount, some items are
: already deleted. But what is the "elegant" solution.
:
: Thanks in advance,
:
: Richard Douwes
:
:



Thu, 07 Aug 2003 07:32:41 GMT  
 Newbie q: Remove selected items from multiselect listbox?
...but don't forget to write
For t = List1.ListCount-1 To 0 Step -1
to make it work.

Florian


Quote:
> Do it in reverse .. this way t will always be = or < than count.

> For t = List1.ListCount -1 To 0
>   If List1.Selected(t) Then
>     List1.RemoveItem (t)
>   End If
> Next t

> --

> Randy Birch
> MVP Visual Basic

> Take the vb.net poll at:
> http://www.mvps.org/vbnet/
> http://www.mvps.org/ccrp/

> Please respond only to the newsgroups so all can benefit.



> : What is the best solution for removing selected items (I used
> Multiselect=1
> : <simple>) from a listbox?
> :
> : I *know* this doesn't work:
> :
> : For t = 1 To List1.ListCount()
> :   If List1.Selected(t - 1) Then
> :     List1.RemoveItem (t - 1)
> :   End If
> : Next t
> :
> : because by the time the counter reaches List1.Listcount, some items are
> : already deleted. But what is the "elegant" solution.
> :
> : Thanks in advance,
> :
> : Richard Douwes
> :
> :



Thu, 07 Aug 2003 08:38:05 GMT  
 Newbie q: Remove selected items from multiselect listbox?
<whoops> ... I was thinking that, just forgot to type it. Thanks for picking
it up.

--

Randy Birch
MVP Visual Basic

Take the vb.net poll at:
http://www.mvps.org/vbnet/
http://www.mvps.org/ccrp/

Please respond only to the newsgroups so all can benefit.


: ...but don't forget to write
: For t = List1.ListCount-1 To 0 Step -1
: to make it work.
:
: Florian
:


: > Do it in reverse .. this way t will always be = or < than count.
: >
: > For t = List1.ListCount -1 To 0
: >   If List1.Selected(t) Then
: >     List1.RemoveItem (t)
: >   End If
: > Next t
: >
: >
: > --
: >
: > Randy Birch
: > MVP Visual Basic
: >
: > Take the vb.net poll at:
: > http://www.mvps.org/vbnet/
: > http://www.mvps.org/ccrp/
: >
: > Please respond only to the newsgroups so all can benefit.
: >
: >
: >


: > : What is the best solution for removing selected items (I used
: > Multiselect=1
: > : <simple>) from a listbox?
: > :
: > : I *know* this doesn't work:
: > :
: > : For t = 1 To List1.ListCount()
: > :   If List1.Selected(t - 1) Then
: > :     List1.RemoveItem (t - 1)
: > :   End If
: > : Next t
: > :
: > : because by the time the counter reaches List1.Listcount, some items
are
: > : already deleted. But what is the "elegant" solution.
: > :
: > : Thanks in advance,
: > :
: > : Richard Douwes
: > :
: > :
: >
: >
:
:



Thu, 07 Aug 2003 08:57:14 GMT  
 Newbie q: Remove selected items from multiselect listbox?

Quote:

> Datum: zondag 18 februari 2001 0:32
> Do it in reverse .. this way t will always be = or < than count.

Thanks for a great tip! I *knew* there had to be a nice little and simple
solution.

Quote:
> For t = List1.ListCount -1 To 0
>   If List1.Selected(t) Then
>     List1.RemoveItem (t)
>   End If
> Next t

I added the "step -1" from the follow-up, and I'll give it a go! Thanks
again, and I hope you friendly folk will be around for my next stupid
beginner's question! :-)

Richard Douwes



Thu, 07 Aug 2003 14:58:31 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. how to remove selected items (multiselect) in listbox

2. how to remove selected items (multiselect) in listbox

3. Selecting All Items In a MultiSelect ListBox

4. Listbox - multiselect : how to get selected items ?

5. ListBox - finding which items are selected with the MultiSelect Property

6. Multiselect listbox: How to SET selected item

7. Remove selected item from listbox

8. Removing the selected item from a listbox

9. how to remove highlight feature at multiselect listbox ??

10. Delete Item in an array which corresponds to Itemsselected in multiselect listbox

11. MultiSelect ListBox Defaults to First Item

12. Capturing item values from multiselect listbox

 

 
Powered by phpBB® Forum Software