|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Remove items from listboxHowever, 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
Show quote
Hide quote
"Bill Nguyen" <billn_nospam_please@jaco.com> wrote in message Have you used the debugger to step through your code in the event procedure? 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 > 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. 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. > 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 > >
Is synclock needed in DLL?
2 easy questions getting row index of dataset Howto: use mutliple languages C++ in VB.net translation from c# Compiler errors due to temp file inconsistencies ?? Object reference not set to an instance of an object. How to get assembly file info? cast sortedlist value as hashtable How to remove if blank in Detail in Crystal Report |
|||||||||||||||||||||||