Just a beginner asking for guide. :)

private void button2_Click(object sender, EventArgs e)
            for (int i = 0; i < listBox1.Items.Count; i++)

This is the method I made for moving all the items in a listbox to the one next to it. Apparently there's a problem with this code, because when I try out the button, one out of three items in the listBox1 gets left behind for some reason. I don't really have time to make screenshots but here's what happens:

There are three items in listBox1, "Football, Baseball, Basketball", and when I use the button "Baseball" gets left behind. Doing vice versa, as in from listBox2 to 1, out of five items in listBox2(three were original items and the other two were moved from lilstBox1), only three of them get moved, leaving other items in listBox2. I cannot find any pattern in the movements.

I hope to find out what's wrong with my code and also the right code for my purpose. Thanks in advance. :D

7 Years
Discussion Span
Last Post by samueal

Your flow is wrong.
Just slowly think, You first add the value of listbox1[0] to listbox2. and you remove the 0th value,

After you remove listbox1[0]th value, the next value moves up
after incrementing ith value you add the listbox1[1] to listbox2
That's why a value is left out

Edited by samueal: n/a


Ah, I get it now. I probably wasn't thinking enough. I changed the code to this and it worked:

private void button2_Click(object sender, EventArgs e)
            int j = listBox1.Items.Count;
            for (int i = 0; i < j; i++)

Thanks for the reply.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.