VB6 Treeview Image Loop changing image

Viewed 343

Given the following Images:

image = 4 is green icon

image = 3 is red icon

My ii index is not moving onto next item, it shows the same index as it passes through the loop as I have checked by stepping through each loop.

I want to change all child items icon to Image = 3

Dim FoundIt As Boolean, ii As Integer, ix As Integer
Dim NodX As Node, NodX2 As Node

On Error Resume Next

For Each NodX2 In TreeView2.Nodes

    If NodX2.Parent.Image = 4 Then
        ii = NodX2.Child.Index
        TreeView2.Nodes(ii).Parent.Child.Image = 3
        Debug.Print ii ' when i step through it repeats the same index,only the first child changes to image = 3
        Pause 0
    End If
Next
1 Answers

Your first For Each loop should use NodX. Inside that loop you can iterate through all children of NodX using NodX2:

Dim objNode As Node
Dim objChildNode As Node
Dim iCounter As Integer
Dim fProceed As Boolean

On Error Resume Next

For Each objNode In TreeView2.Nodes

    If objNode.Image = 4 Then

        ' Check for Children
        If objNode.Children > 0 Then

            ' Get first Child
            Set objChildNode = objNode.Child

            ' Initialize flag
            fProceed = True

            ' Loop through all children
            For iCounter = 1 To objNode.Children

                ' Set image to 3 if it was 5
                If objChildNode.Image <> 5 Then
                    fProceed = False
                    Exit For
                End If

                ' Get next node
                Set objChildNode = objChildNode.Next

            Next

            If fProceed Then

                ' Get first Child again
                Set NodX2 = NodX.Child

                ' Loop through all children
                For iCounter = 1 To objNode.Children

                    ' Set image to 3
                    objChildNode.Image = 3

                    ' Get next node
                    Set objChildNode = objChildNode.Next

                Next

            End If

        End If

    End If

Next
Related