Click here to Skip to main content
15,888,102 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
my code is as follows
i want that i select some items from the list box and the selected items are automatically add on to another list box and remove from the first list box .
in asp.net using c#.

What I have tried:

protected void ListBox3_SelectedIndexChanged(object sender, EventArgs e)
{

}
protected void ListBox4_SelectedIndexChanged(object sender, EventArgs e)
{

}
protected void Button6_Click(object sender, EventArgs e)
{
int i;
for (i = 0; i <= ListBox3.Items.Count; i++)
{
if (ListBox3.Items[i].Selected == true)
{
ListBox4.Items.Add(ListBox3.SelectedItem);
ListBox3.Items.Remove(ListBox3.SelectedItem);
}
}









}
}
Posted
Updated 23-Jan-18 2:02am
Comments
F-ES Sitecore 23-Jan-18 8:06am    
You're looping through the number of items in ListBox3 (let's say there are 5) and you arte removing them as they go (let's say you remove 2 so there are 3 left), when you hit i being three it looks for the fourth item but there are only 3. There used to be 5 but you've removed them.

Break the loop when you find a selected item and u use a "while" loop or some other mechanism that loops until nothing in the listbox is selected. An alternative is to add the selected items to a new list and once you've looped through them the listbox, loop through the new list you've made and remove\add in that loop.
Richard Deeming 23-Jan-18 10:54am    
Probably simpler just to reverse the loop:
for (int i = ListBox3.Items.Count - 1; i >= 0; i--) { ...


I'd assume, since the condition is tested each time, and the code isn't caching the count anywhere, that it wouldn't try to iterate past the end of the list. (So long as the termination condition has been fixed, as Jochen spotted.)

But iterating forwards will still end up skipping items.
F-ES Sitecore 23-Jan-18 11:35am    
Given he is adding the items to another control I'd assume he'd want to preserve the order of items, if so he'd need to insert at the front of the collection too, but if it was the second time the code has run that might also have undesirable results. Things always seem so simple in your mind before you start to actually code :)

1 solution

The hint is in the "less than size of the collection" part of the error message because you are iterating up to and including that size:
C#
for (i = 0; i <= ListBox3.Items.Count; i++)
It must be
C#
for (i = 0; i < ListBox3.Items.Count; i++) 


[EDIT]
As noted by Richard, your loop might skip items and you should iterating down starting at the last item.
[/EDIT]
 
Share this answer
 
v2
Comments
Richard Deeming 23-Jan-18 10:55am    
That's still going to end up skipping items. :)
Jochen Arndt 23-Jan-18 11:02am    
Yes. I recognised that too meanhwile.

But the question was "how to solve the index out of range error" :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900