Aidan Kehoe Aidan Kehoe - 4 months ago 58x Question Save which nodes were expanded in TreeView

I am creating a form in that contains a TreeView. This TreeView is populated with expandable parent nodes and corresponding child nodes. When the user runs the program, they can expand any nodes they want. Is there a way to programmatically save which nodes were expanded so that next time the form is loaded, those nodes remain expanded? I don't necessarily need coded examples (although they can be useful), just a general idea of the commands/controls/logic to use in the program. Thank you in advance!


Here are some basic ideas to guide your implementation:

Create a List to keep track of the nodes that are currently expanded:

Private _ExpandedNodeList As New List(Of String)

Set-up two event handlers to track the AfterCollapse and AfterExpand events of the TreeView - these happen when a user opens/ closes different parts of the tree. The trick here is that the event handlers just add and remove the Node's tag value which is some string you've set on the node when you built the tree.

Private Sub TreeView_AfterCollapse(sender As Object, e As TreeViewEventArgs) Handles TreeView.AfterCollapse
    If _ExpandedNodeList.Contains(e.Node.Tag.ToString) Then
    End If
End Sub
Private Sub TreeView_AfterExpand(sender As Object, e As TreeViewEventArgs) Handles TreeView.AfterExpand
    If Not _ExpandedNodeList.Contains(e.Node.Tag.ToString) Then
    End If
End Sub

Now, let's assume that you have a point in the application where you need to restore the TreeView to a particular state. You can recurse the nodes in the TreeView and call Expand on the ones that are in your list.

My implementation looks like this:

Private Sub ShowVisibleNodes()
    Dim Node As TreeNode

    For Each ExpandedChildNodeName As String In _ExpandedNodeList
        For Each RootNode As TreeNode In TreeView.Nodes
            Node = NodeFromTagName(ExpandedChildNodeName, RootNode)
            If Node IsNot Nothing Then
            End If

End Sub
Private Function NodeFromTagName(ByVal NodeTagName As String, NodeToSearch As TreeNode) As TreeNode
    Dim NextNode As TreeNode

    'check input node
    If NodeToSearch.Tag IsNot Nothing Then
        If NodeToSearch.Tag.Equals(NodeTagName) Then
            Return NodeToSearch
        End If
    End If

    'check child nodes and recurse on intermediary nodes
    For Each Node As TreeNode In NodeToSearch.Nodes
        If Node.Tag IsNot Nothing Then
            If Node.Tag.Equals(NodeTagName) Then
                Return Node
            End If
            NextNode = NodeFromTagName(NodeTagName, Node)
            If NextNode IsNot Nothing Then
                Return NextNode
            End If
        End If

    Return Nothing

End Function

Note this code isn't guaranteed to work simply by dropping it in your application.

At least, you need to think carefully about building your tree and whether or not you set a value on the Tag for each TreeNode.

Hopefully this will get you started.