How to delete multiple checked items from CheckedListBox
Asked Answered
D

6

5

I know how to remove a single checkedItem from checkedlistbox. But now, I want to remove all the checked items in one go.

I tried this:

foreach (var item in List_Frente.CheckedItems)
            {
                List_Frente.Items.Remove(item);
            }

But as you probably know, it gives me an error,saying that, List that this enumerator is bound to has been modified. An enumerator can only be used if the list does not change. How may I remove all checkeditems with a single click ?

Decennary answered 29/1, 2013 at 19:31 Comment(7)
Don't use a foreach. Use a for.Coryza
Im not familiar with winforms, but can you do a List_Frente.CheckedItems.ToList()? if you can that will solve the problemBraud
@HighCore not, just ToString() =(Decennary
@HighCore foreach uses an iterator. You cannot make changes to a collection while there is an active iterator. You cannot do this, in its current form, with a foreach.Coryza
@mikeTheLiar but how would I get the value of the checkeditems without a foreach ?Decennary
for (i = 0; i < (List_Frente.Items.Count ; i++) Check out this page: msdn.microsoft.com/en-us/library/e954th47.aspxCoryza
possible duplicate of How do I do I loop through items in a list box and then remove that item?Gurdwara
A
11

you could do something like this:

foreach (var item in List_Frente.CheckedItems.OfType<string>().ToList())
{
    List_Frente.Items.Remove(item);
}

If you want to write it all in one line:

List_Frente.CheckedItems.OfType<string>().ToList().ForEach(List_Frente.Items.Remove);

This only works if your items are of the same type, of course. Looks still crude, though.

Edit-Explanation: ToList() creates a new list and thus, the original CheckedItems list can be changed, as the enumerator now enumerates our newly created list, not the original. The OfType<string>() is only there because we need something to call ToList() on.

Alonzo answered 29/1, 2013 at 19:40 Comment(5)
OMG WORKED !! Thanks Jobo !Decennary
Your welcome, it would help all here if you would accept the answer then :)Alonzo
I know, I was just making some tests, and I have to wait a few minuts before mark as the answer ;P ... Could you explain me why worked this way ? And not the way I did ?Decennary
@Decennary because the LinQ methods OfType<T> and ToList() create a new instance of a List and then you iterate on that. Instead of iterating the original collection, which will produce an exception if you removed an item from it while iterating.Braud
@Alonzo Nice one! You could add the LINQ-Variant to your answer: List_Frente.CheckedItems.OfType<string>().ToList().ForEach(List_Frente.Items.Remove);Mesmerize
I
4
while (checkedListBox.CheckedItems.Count > 0) {
   checkedListBox.Items.RemoveAt(checkedListBox.CheckedIndices[0]);
}
Intravasation answered 30/5, 2015 at 20:11 Comment(1)
+1 Can confirm this works. Kinda feel silly for not seeing it myself though, maybe that's why this didn't have any votes? Either way, thank you.Deprived
M
2

This is because you modify the list you are iterating over. Use a for-statement to prevent this from happening:

for(var i=0; i<List_Frente.CheckedItems.Count; i++)
{
    ((IList)List_Frente.CheckedItems).Remove(List_Frente.CheckedItems[0]);
}

As stated in this MSDN-Article the CheckedListBox.CheckedItemCollection implements the IList.Remove-method explicitly, meaning you will have to cast the instances to IList to make this work.

Mesmerize answered 29/1, 2013 at 19:37 Comment(5)
Hi ! There's no such method as ChedkItems.REMOVE() . I tried but it doesnt exist ;\Decennary
Oh yeah, there's no ToList() as well ;sDecennary
Huh. I took a closer look a the regarding MSDN article which clearly states that there is a Remove-Method - except that it needs to be implemented explicitly. I will modify my answer accordingly.Mesmerize
@Decennary I fixed the code. These classes introduced in the very early versions of .NET sometimes don't have all the comfort the new ones have ;)Mesmerize
@Mesmerize thanks you too man ! I'll save your code on my codes.txt hehe xD +1Decennary
P
2

Might be an addon for this post.

To clear all the items.

checkedListBox1.Items.Clear();
Prehistoric answered 8/9, 2015 at 9:25 Comment(0)
A
0

This works.

object[] items = new object[checkedListBox.Items.Count];
checkedListBox.Items.CopyTo(items, 0);
checkedListBox.Items.Clear();
checkedListBox.Items.AddRange(items);

I aim to return with an solution that are more beautiful and less hacky.

Apulia answered 29/1, 2013 at 20:1 Comment(0)
C
0

you can try following solution:

    For i As Integer = checkedListBox.Items.Count - 1 To 0 Step -1
        If checkedListBox.GetItemChecked(i) Then
            checkedListBox.Items.RemoveAt(i)
        End If
    Next
Cyclic answered 29/11, 2022 at 10:23 Comment(1)
Be careful with that. You change the content of the list you are iterating over.Neptunium

© 2022 - 2024 — McMap. All rights reserved.