Solved

Looping through nodes in a tree

Posted on 2006-11-17
5
392 Views
Last Modified: 2007-12-19
I have a VB6 program with a tree in a form. I already have the tree all populated. What I want to do know is build in the logic that if all of the children of a parent are checked then the parent itself will be checked, or true. If someone could please point me in the right direction with a sample of code, that would be great. Thanks, arajoe.
0
Comment
Question by:arajoe
5 Comments
 
LVL 142

Assisted Solution

by:Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3] earned 50 total points
ID: 17968422
this recursive sub would set the same checked value for all the children by the parent's checked value.

public Sub CheckChildren ( byval n as node )
  dim c as Node

  set c = n.Child
  while not ( c is nothing )
    c.checked = n.checked
    checkchildren c

    set c = c.nextsibling
  wend


end sub
0
 
LVL 13

Accepted Solution

by:
jmundsack earned 350 total points
ID: 17968691
Drop a treeview on a form and paste in this code:

Option Explicit

Private Sub Form_Load()
    Dim nod As Node
    With TreeView1
        Set nod = .Nodes.Add(, tvwFirst, "fruit", "Fruit")
        nod.Expanded = True
        .Nodes.Add "fruit", tvwChild, "apple", "Apple"
        .Nodes.Add "fruit", tvwChild, "banana", "Banana"
        .Nodes.Add "fruit", tvwChild, "orange", "Orange"
        Set nod = .Nodes.Add(, tvwFirst, "veg", "Vegetable")
        nod.Expanded = True
        .Nodes.Add "veg", tvwChild, "asparagus", "Asparagus"
        .Nodes.Add "veg", tvwChild, "cucumber", "Cucumber"
        .Nodes.Add "veg", tvwChild, "zucchini", "Zucchini"
    End With
End Sub

Private Sub TreeView1_NodeCheck(ByVal Node As MSComctlLib.Node)
    Dim nod As Node
    Dim blnAll As Boolean
    If Node.Children > 0 Then
        Set nod = Node.Child
        Do While Not nod Is Nothing
            nod.Checked = Node.Checked
            Set nod = nod.Next
        Loop
    End If
    If Not Node.Parent Is Nothing Then
        blnAll = True
        Set nod = Node.Parent.Child
        Do While Not nod Is Nothing
            If Not nod.Checked Then
                blnAll = False
                Exit Do
            End If
            Set nod = nod.Next
        Loop
        Node.Parent.Checked = blnAll
    End If
End Sub

0
 
LVL 85

Assisted Solution

by:Mike Tomlinson
Mike Tomlinson earned 50 total points
ID: 17969104
0
 
LVL 27

Assisted Solution

by:Ark
Ark earned 50 total points
ID: 17969674
Another approach can be used if you have no duplicate nodes in each level in treeview:

Private Sub Form_Load()
   Dim nd As Object, ndParent As Object
   Dim i As Long, j As Long, k As Long
   For i = 1 To 5
      Set ndParent = TreeView1.Nodes.Add(, , , "Node Level 1 #" & i)
      For j = 1 To 5
          Set nd = TreeView1.Nodes.Add(ndParent, tvwChild, , "Node Level 2 #" & j)
          For k = 1 To 5
              Call TreeView1.Nodes.Add(nd, tvwChild, , "Node Level 3 #" & j)
          Next k
      Next j
   Next i
End Sub

Private Sub TreeView1_NodeCheck(ByVal Node As Node)
   Dim sPath As String, length As Long
   sPath = Node.FullPath
   length = Len(sPath)
   For Each nd In TreeView1.Nodes
       If Len(nd.FullPath) >= length Then
          If Left(nd.FullPath, length) = sPath Then
             nd.Checked = Node.Checked
          End If
       End If
   Next
End Sub
0
 

Author Comment

by:arajoe
ID: 17980060
Thanks to everyone. I went with the answer I did because it allowed for a more recursive function. The code I ended up with is below. Thanks again, arajoe.


Private Sub Check_Parents(ByRef Node As MSComctlLib.Node, ByVal Node_Checked_Value As Boolean)

    Dim Child As MSComctlLib.Node
    Dim All_Checked As Boolean
   
    If Node.Children > 0 Then
        Set Child = Node.Child
        Do While Not Child Is Nothing
       
            Child.Checked = Node_Checked_Value
           
            UnAssign_SUP Child
           
            Check_Parents Child, Child.Checked
           
            Set Child = Child.Next
           
        Loop
    End If
   
    If Not Node.Parent Is Nothing Then
        All_Checked = True
        Set Child = Node.Parent.Child
        Do While Not Child Is Nothing
            If Not Child.Checked Then
                All_Checked = False
                Exit Do
            End If
            Set Child = Child.Next
        Loop
        Node.Parent.Checked = All_Checked
    End If
   
End Sub
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now