[code]for (int d = 0; d <= checkedListBox1.Items.Count -1; d++)
{
if (checkedListBox1.GetItemChecked(d) == true)
{
checkedListBox1.Items.RemoveAt(d);
}
}[/code]
The for loop is completed before it's suppose to end... Why is this? I checked all the items (~20) in the checkedListBox, but when I run this code, not all the items are removed; some are left over. Why is this? I even tried removing if(checkedListBox1.GetItemChecked).
For loop is exited before completion
3 Answers
Know the answer? Post it. Somebody with the same question will find it here.

FroglegRank #1,020 · 1.9K reputationPosted Mar 1, 2011, 5:51 PM EST
for (int d = checkedListBox1.Items.Count - 1; d >= 0; d--)
{
if (checkedListBox1.GetItemChecked(d))
{
checkedListBox1.Items.RemoveAt(d);
}
}
Sam HobbsRank #107 · 20.5K reputationPosted Mar 1, 2011, 7:18 PM EST
Assuming that Frogleg's suggestion works, an alernative would be to use foreach instead of for. If Frogleg's suggestion does not work, then try using foreach.
FroglegRank #1,020 · 1.9K reputationPosted Mar 1, 2011, 7:34 PM EST
checkedlistbox1 contains 10 items numbers 1 - 10
you check item 1,2 & 3 and with marks code you first remove item 1
Item 2 is now becomes item 1
The code loops to remove item 2 which is now item 3 so item 2 remains
With the code I supplied we first remove item 3, so that item 1 & 2 are still in their original positions
Then we remove 2, lastly 1
Hope this helps you