467,165 Members | 988 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,165 developers. It's quick & easy.

Remove items from listbox

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
Nov 21 '05 #1
  • viewed: 5753
Share:
3 Replies

"Bill Nguyen" <bi*****************@jaco.com> wrote in message
news:uv**************@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.
Nov 21 '05 #2
What was the error ?

rawCoder

"Bill Nguyen" <bi*****************@jaco.com> wrote in message
news:uv**************@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

Nov 21 '05 #3
Thanks Peter.

ArrayList seems to be the one I needed.

Bill

"Peter van der Goes" <p_**********@toadstool.u> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...

"Bill Nguyen" <bi*****************@jaco.com> wrote in message
news:uv**************@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.

Nov 21 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Supra | last post: by
4 posts views Thread by Bilo | last post: by
4 posts views Thread by mostafa atalla | last post: by
3 posts views Thread by Hrcko | last post: by
1 post views Thread by deepak | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.