Quantcast
Channel: VBForums - CodeBank - Visual Basic 6 and earlier
Viewing all articles
Browse latest Browse all 1449

"Open" TreeView Helper Procedures thread

$
0
0
I'm starting this thread as a place where people might post "TreeView helper procedures". I'm just making extensive use of TreeView these days, and have developed quite a library of "helper procedures", and I'm interested in what others have developed.

I'm actually using the VB6 TreeView, but I believe everything I've done would also work with the VB5 version (and hopefully Krool's version as well).

I'll get things rolling with three I've recently developed (and the first one is actually dependent on the second & third).

Code:


Public Sub CopyTree(tvwSource As TreeView, tvwDest As TreeView)
    ' The tvwDest is cleared before copying.
    ' Also, the nodes are added to tvwDest in the true vertical order of the tvwSource.
    ' Only the hierarchy and the actual Node.Tag strings are copied.  Formatting is not.
    '
    Dim coll As Collection: Set coll = OrderedTreeviewNodes(tvwSource)
    Dim oSorc As Node, oDest As Node, oPrevDest As Node
    Dim iPrevLevel As Long      ' Initially zero.
    Dim iThisLevel As Long      ' Initially zero.
    '
    tvwDest.Nodes.Clear
    For Each oSorc In coll
        If tvwDest.Nodes.Count = 0& Then
            ' Just to get things started.
            Set oDest = tvwDest.Nodes.Add(, , , oSorc.Text)
        Else
            iThisLevel = NodeNestingLevel(oSorc)
            '
            ' If we're too deep, back out.
            If iThisLevel < iPrevLevel Then
                Do While iThisLevel < iPrevLevel
                    Set oPrevDest = oPrevDest.Parent
                    iPrevLevel = iPrevLevel - 1&
                Loop
            End If
            '
            ' Now add it.
            If iThisLevel = iPrevLevel Then
                Set oDest = tvwDest.Nodes.Add(oPrevDest, tvwNext, , oSorc.Text)
            Else                                ' It's a child.  It can only be one greater.
                Set oDest = tvwDest.Nodes.Add(oPrevDest, tvwChild, , oSorc.Text)
            End If
        End If
        '
        ' Save info for next loop.
        oDest.Tag = oSorc.Tag                  ' MUST move tag data.
        Set oPrevDest = oDest
        iPrevLevel = iThisLevel
    Next
End Sub


Code:


Public Function OrderedTreeviewNodes(tvw As TreeView, Optional oNode As Node, Optional coll As Collection, Optional bRecursing As Boolean) As Collection
    ' This always starts at oNode.  If oNode Is Nothing, then the first node is dug out.
    ' Initial caller should never put anything in coll nor bRecursing arguments.  Those are just for recursion.
    ' It's easiest to just call like:  Set coll = OrderedTreeviewNodes(tvw)
    '
    If Not bRecursing Then                          ' Get things going.
        If tvw.Nodes.Count = 0& Then
            Set OrderedTreeviewNodes = New Collection
            Exit Function                          ' Returns empty instantiated collection.
        End If
        Set coll = New Collection                  ' We'll eventually return this coll object.
        If oNode Is Nothing Then
            Set oNode = tvw.Nodes(1&)              ' May or may not be the top.
            Do Until (oNode.Parent Is Nothing)      ' Loop to get to top of nesting level.
                Set oNode = oNode.Parent
            Loop
            Do Until (oNode.Previous Is Nothing)    ' Loop to get to very top.
                Set oNode = oNode.Previous
            Loop
        End If
    End If
    '
    ' Ok, we've got a node, so traverse this node level.
    Do While Not oNode Is Nothing
        coll.Add oNode
        '
        ' If the node has children, they come next.
        If oNode.Children Then
            OrderedTreeviewNodes tvw, oNode.Child, coll, True  ' Recursion.
        End If
        '
        ' And now we can move on.
        Set oNode = oNode.Next
    Loop
    '
    ' See if we're recursing.  If not, return our collection.
    ' Otherwise, recursion doesn't use the return.
    If Not bRecursing Then Set OrderedTreeviewNodes = coll
End Function


Code:


Public Function NodeNestingLevel(oNode As Node) As Long
    If oNode Is Nothing Then NodeNestingLevel = -1&: Exit Function
    ' Zero is the top.  -1 is when no node as passed in.
    Dim o As Node
    Set o = oNode
    Do Until (o.Parent Is Nothing)
        NodeNestingLevel = NodeNestingLevel + 1&
        Set o = o.Parent
    Loop
End Function



Viewing all articles
Browse latest Browse all 1449

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>