Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17


Treeview Control

Posted on 1999-07-07
Medium Priority
Last Modified: 2010-05-02
I am trying to loop through a treeview control and return the order of treeview control as it appears on the screen.  For example if the first node has three children, I would like the routine to return the text of the child nodes second third and forth.  I need to do this because we are using the control to allow a user to set the layout of a report in a more visual manner.  I have tried to use the index property, however the child nodes return after all of the root nodes.  This routine would have to be able to go at least six levels down for each root node and some root nodes would have no children.  Please contact with any further questions about this problem.
Question by:tkuppinen
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2

Expert Comment

ID: 1522545
for each node check if it has children i.e.
TreeView1.Nodes(i).Children > 0
if yes use
Node.FirstSibling.Index and Node.LastSibling.Index property to go on...


Author Comment

ID: 1522546
I have tried this approach before.  I feel like I am missing one step in this procedure.  The code is placed below.

Public Sub setOrder()
  Dim myNode As Node
    Dim i As Integer, o As Integer, n As Integer
    o = 1
    i = 1
    Dim b As Boolean, strText As String
    b = True
    For Each myNode In TreeView1.Nodes
    On Error GoTo errhandler
    If TreeView1.Nodes.Item(i).Parent Is Nothing Then
    db.Execute "Update tblword set [orders]= " & o & " Where [Bookmark]= '" & TranslateSQL(TreeView1.Nodes.Item(i).Text) & "'" & ""
        If TreeView1.Nodes.Item(i).Children > 0 Then
        n = TreeView1.Nodes(i).FirstSibling.Index
        strText = TreeView1.Nodes(i).FirstSibling.Text
            Do Until b = False
                If n <> TreeView1.Nodes(i).LastSibling.Index Then
                db.Execute "Update tblword set [orders]= " & o & " Where [Bookmark]= '" & TranslateSQL(strText) & "'" & ""
                n = TreeView1.Nodes.Item(i).Next.Index
                strText = TreeView1.Nodes.Item(n).Next.Text
                o = o + 1
                i = i + 1
                b = False
                Exit Do

                End If

        End If
    End If
    i = i + 1
    o = o + 1
    Next myNode
Exit Sub
End Sub


Accepted Solution

Tios earned 320 total points
ID: 1522547
Dear tkuppinen the solution is not so simple but i will try to explain you. You must put your mind to think recursively.
I make an examle which print the treeview at debug object.
Just put the function in the body of your program as
  Dim dummy as Integer
  dummy = Tree_Visit(1)

Function Tree_Visit(ind_of_Node As Integer) As Integer
 Dim nd As Node
 Dim i As Integer
 Debug.Print Trv1.Nodes(ind_of_Node).Text
 If Trv1.Nodes(ind_of_Node).Children > 0 Then
   Set nd = Trv1.Nodes(ind_of_Node).Child
   i = nd.Index
   i = Tree_Visit(i)
   Tree_Visit = i
   If Trv1.Nodes(ind_of_Node).Next Is Nothing Then
     Set Trv1.Nodes(ind_of_Node).Parent
     If nd Is Nothing Then
       Tree_Visit = ind_of_Node
       Exit Function
     End If
     If Not (nd Is Trv1.Nodes(1).LastSibling) Then
       Set nd = nd.Next
       i = nd.Index
       i = Tree_Visit(i)
     End If
     Tree_Visit = i
     Set nd = Trv1.Nodes(ind_of_Node).Next
     i = nd.Index
     i = Tree_Visit(i)
     Tree_Visit = i
   End If
 End If
End Function

I test this in a treeview with Depth = 2 Levels and works.
I believe that works with n Levels but i do not have the time to test.
I hope to help you.


Author Comment

ID: 1522548
Adjusted points to 85

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses

704 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