0
votes

I'm trying to clear items from a listbox that currently appear as child nodes in a treeview. I've come up with storing the child nodes in an array(which works) then thought I would loop through and remove the listbox item if it appears in the array (basically the treenodes are added from double clicking the treeview and are placed under a dated parent node).

When I run the code I get the following error message on the final Next

List that this enumerator is bound to has been modified. An enumerator can only be used if the list does not change.

I know this is to do with the For Each loop however I'm struggling as a novice to work it out.

Dim ndes As New ArrayList
Dim no As TreeNode
    For Each no In tvProgress.Nodes

        Dim CNode As TreeNode
        For Each CNode In no.Nodes
            ndes.Add(CNode.Text)
        Next
    Next

    Dim ditem As Object

    For Each ditem In lstPlanned.Items
        If ndes.Contains(ditem) Then
            lstPlanned.Items.Remove(ditem)
        End If
    Next
1

1 Answers

1
votes

You need a diff loop:

For i As Integer = (lstPlanned.Items.Count - 1) To 0 Step -1
    If ndes.Contains(lstPlanned.Items(i)) Then
        lstPlanned.Items.Remove(lstPlanned.Items(i))
        Exit For
    End If
Next