Home All Groups Group Topic Archive Search About

Remove items from listbox

Author
7 Apr 2005 2:18 PM
Bill Nguyen
I use the following example (from another post) and it seemed to work fine.
However, when I add the syntax to remove the selected item from the
senderbox, I got error.
senderBox.Items.RemoveAt(senderbox.selectedIndex)

I was told that I need to copy selecteditems to an array and run the For
each  routine from the array instead. Can someone please give me some idea
on how to accomplish this?
Thanks a million.

Bill


Private Sub lb1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles lb1.SelectedIndexChanged

Dim o As Object

Dim senderBox As ListBox

senderBox = DirectCast(sender, ListBox)

lb2.Items.Clear()

For Each o In senderBox.SelectedItems

lb2.Items.Add(o)

' to remove from senderBox

senderBox.Items.RemoveAt(senderbox.selectedIndex)


Next

End Sub

Author
7 Apr 2005 4:02 PM
Peter van der Goes
Show quote Hide quote
"Bill Nguyen" <billn_nospam_please@jaco.com> wrote in message
news:uvcIzy3OFHA.2356@TK2MSFTNGP14.phx.gbl...
>I use the following example (from another post) and it seemed to work fine.
>However, when I add the syntax to remove the selected item from the
>senderbox, I got error.
> senderBox.Items.RemoveAt(senderbox.selectedIndex)
>
> I was told that I need to copy selecteditems to an array and run the For
> each  routine from the array instead. Can someone please give me some idea
> on how to accomplish this?
> Thanks a million.
>
> Bill
>
>
> Private Sub lb1_SelectedIndexChanged(ByVal sender As System.Object, ByVal
> e
> As System.EventArgs) Handles lb1.SelectedIndexChanged
>
> Dim o As Object
>
> Dim senderBox As ListBox
>
> senderBox = DirectCast(sender, ListBox)
>
> lb2.Items.Clear()
>
> For Each o In senderBox.SelectedItems
>
> lb2.Items.Add(o)
>
> ' to remove from senderBox
>
> senderBox.Items.RemoveAt(senderbox.selectedIndex)
>
>
> Next
>
> End Sub
>
Have you used the debugger to step through your code in the event procedure?
Take note of the value of selectedIndex the second time through your loop.
As the item related to the selected index has been removed, my test
indicates the value of SelectedIndex is now -1, which throws the
System.ArgumentOutOfRangeException.
I'd recommend you look at the ArrayList class instead of using an array.

--
Peter [MVP Visual Developer]
Jack of all trades, master of none.
Author
7 Apr 2005 11:46 PM
Bill Nguyen
Thanks Peter.

ArrayList seems to be the one I needed.

Bill

Show quoteHide quote
"Peter van der Goes" <p_vandergoes@toadstool.u> wrote in message
news:%23eIr9s4OFHA.2736@TK2MSFTNGP09.phx.gbl...
>
> "Bill Nguyen" <billn_nospam_please@jaco.com> wrote in message
> news:uvcIzy3OFHA.2356@TK2MSFTNGP14.phx.gbl...
>>I use the following example (from another post) and it seemed to work
>>fine. However, when I add the syntax to remove the selected item from the
>>senderbox, I got error.
>> senderBox.Items.RemoveAt(senderbox.selectedIndex)
>>
>> I was told that I need to copy selecteditems to an array and run the For
>> each  routine from the array instead. Can someone please give me some
>> idea on how to accomplish this?
>> Thanks a million.
>>
>> Bill
>>
>>
>> Private Sub lb1_SelectedIndexChanged(ByVal sender As System.Object, ByVal
>> e
>> As System.EventArgs) Handles lb1.SelectedIndexChanged
>>
>> Dim o As Object
>>
>> Dim senderBox As ListBox
>>
>> senderBox = DirectCast(sender, ListBox)
>>
>> lb2.Items.Clear()
>>
>> For Each o In senderBox.SelectedItems
>>
>> lb2.Items.Add(o)
>>
>> ' to remove from senderBox
>>
>> senderBox.Items.RemoveAt(senderbox.selectedIndex)
>>
>>
>> Next
>>
>> End Sub
>>
> Have you used the debugger to step through your code in the event
> procedure? Take note of the value of selectedIndex the second time through
> your loop. As the item related to the selected index has been removed, my
> test indicates the value of SelectedIndex is now -1, which throws the
> System.ArgumentOutOfRangeException.
> I'd recommend you look at the ArrayList class instead of using an array.
>
> --
> Peter [MVP Visual Developer]
> Jack of all trades, master of none.
>
Author
7 Apr 2005 4:03 PM
rawCoder
What was the error ?

rawCoder

Show quoteHide quote
"Bill Nguyen" <billn_nospam_please@jaco.com> wrote in message
news:uvcIzy3OFHA.2356@TK2MSFTNGP14.phx.gbl...
> I use the following example (from another post) and it seemed to work
fine.
> However, when I add the syntax to remove the selected item from the
> senderbox, I got error.
> senderBox.Items.RemoveAt(senderbox.selectedIndex)
>
> I was told that I need to copy selecteditems to an array and run the For
> each  routine from the array instead. Can someone please give me some idea
> on how to accomplish this?
> Thanks a million.
>
> Bill
>
>
> Private Sub lb1_SelectedIndexChanged(ByVal sender As System.Object, ByVal
e
> As System.EventArgs) Handles lb1.SelectedIndexChanged
>
> Dim o As Object
>
> Dim senderBox As ListBox
>
> senderBox = DirectCast(sender, ListBox)
>
> lb2.Items.Clear()
>
> For Each o In senderBox.SelectedItems
>
> lb2.Items.Add(o)
>
> ' to remove from senderBox
>
> senderBox.Items.RemoveAt(senderbox.selectedIndex)
>
>
> Next
>
> End Sub
>
>