472,373 Members | 2,126 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,373 software developers and data experts.

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
3 5971

"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Supra | last post by:
in vb6 listbox1.remove item 5 how will i do in vb.net? regards
0
by: Shawn K | last post by:
I populate a listbox with the following code when my form loads: lstBox.Items.Add("value"); Once the page loads, I give the user the ability to remove items from the list box by selecting an...
4
by: Bilo | last post by:
I dont know what i am doing false. the code : private void button2_Click(object sender, System.EventArgs e) { foreach (string filename in listBox1.SelectedItems)...
4
by: mostafa atalla | last post by:
How can I remove some selected items from the listBox in the run time ??
1
by: Dan Bass | last post by:
I'm looking to develop a listbox with in-place editing where as each item is selected, it grows to fit in all the text boxes. When the item is deselected, it shrinks back to its original size. The...
3
by: Hrcko | last post by:
How to remove a selected item from listBox? Hrcko
4
by: Ron | last post by:
I've got a listbox that holds a list of groups. Users can select a group, hit the remove button and the group should be removed from the listbox. The only problem is that no matter which group you...
1
by: deepak | last post by:
hi , i have 2 listbox and 2 buttons like add and remove, both listbox have multiple selection true so that i can either add or remove multiple selected items. the first listbox is populated with...
7
by: Mike Johnson | last post by:
How do I remove a item that's been selected from a listview? I'm using Visual Basic 2005 express edition. Thanks
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was proposed, which integrated multiple engines and...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific technical details, Gmail likely implements measures...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the synthesis of my design into a bitstream, not the C++...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand. Background colors can be used to highlight important...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python has gained popularity among beginners and experts...
1
by: Johno34 | last post by:
I have this click event on my form. It speaks to a Datasheet Subform Private Sub Command260_Click() Dim r As DAO.Recordset Set r = Form_frmABCD.Form.RecordsetClone r.MoveFirst Do If...
1
by: ezappsrUS | last post by:
Hi, I wonder if someone knows where I am going wrong below. I have a continuous form and two labels where only one would be visible depending on the checkbox being checked or not. Below is the...
0
by: jack2019x | last post by:
hello, Is there code or static lib for hook swapchain present? I wanna hook dxgi swapchain present for dx11 and dx9.
0
DizelArs
by: DizelArs | last post by:
Hi all) Faced with a problem, element.click() event doesn't work in Safari browser. Tried various tricks like emulating touch event through a function: let clickEvent = new Event('click', {...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.