[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).
Loading
FroglegPosted Mar 2, 2011, 12:34 AM
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
Sam HobbsPosted Mar 2, 2011, 12:18 AM
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.
FroglegPosted Mar 1, 2011, 10:51 PM
for (int d = checkedListBox1.Items.Count - 1; d >= 0; d--)
{
if (checkedListBox1.GetItemChecked(d))
{
checkedListBox1.Items.RemoveAt(d);
}
}