Subscribe to Cache Cow        RSS Feed
-----

Getting and Setting the Expansion State in a TreeView in VB.NET

Icon Leave Comment
If any of you readers are like me, you are sitting here looking at the TreeView and wondering how you are going to maintain the expansion state of each node. I tested out several possible methods for getting the expansion states and setting them and finally settled on a method which has worked pretty well for me. (I am using .NET framework 4.0.)

To start off, I would like to publicly admonish Microsoft for not providing for an easy way to do this. You would think that it would be simple. Maybe they have a method that is already part of the framework. If so, I apologize for my inability to perform research. I have not, however, been able to find one that is readily available.

To start off, my problem was this: I have an application which iterates through each order and displays some of the order information in a tree view format. The nodes go up to four levels deep, but these methods will work for any depth up to the memory limitation of the objects i use. When the user pages forward or backward through the orders, the treeview is recreated with new information. Since the nodes are cleared when it is recreated, the expansion state information is lost between pages. The methods i use below have alleviated this issue:

    Private Sub Navigation_Click(sender as System.Object, e as System.EventArgs) Handles PageBack.Click, PageForward.Click
        Dim treeViewStates as Queue(Of Boolean) = New Queue(Of Boolean)

        GetExpansionStates(myTreeView, treeViewStates)
        myTreeView.Nodes.Clear()
        FillMyTreeView(myTreeView)
        SetExpansionStates(myTreeView, treeViewStates)
    End Sub

    ''' <summary>
    ''' Gets the expansion states of all nodes in the tree view.
    ''' </summary>
    ''' <param name="tv">The tree view.</param>
    ''' <param name="q">The queue.</param>
    Private Sub GetExpansionStates(ByVal tv As TreeView, ByRef q As Queue(Of Boolean))
        For Each n As TreeNode In tv.Nodes
            GetNodeExpansionState(n, q)
        Next
    End Sub

    ''' <summary>
    ''' Gets the expansion state of the tree node and all of its child nodes.
    ''' </summary>
    ''' <param name="n">The node.</param>
    ''' <param name="q">The queue.</param>
    Private Sub GetNodeExpansionState(ByVal n As TreeNode, ByRef q As Queue(Of Boolean))
        If n.Nodes.Count > 0 Then q.Enqueue(n.IsExpanded)

        For Each no As TreeNode In n.Nodes
            GetNodeExpansionState(no, q)
        Next
    End Sub

    ''' <summary>
    ''' Sets the expansion states of all nodes in the tree view.
    ''' </summary>
    ''' <param name="tv">The tree view.</param>
    ''' <param name="q">The queue.</param>
    Private Sub SetExpansionStates(ByVal tv As TreeView, ByRef q As Queue(Of Boolean))
        For Each n As TreeNode In tv.Nodes
            If n.Nodes.Count > 0 Then SetNodeExpansionState(n, q)
        Next
    End Sub

    ''' <summary>
    ''' Sets the expansion state of the tree node and its child nodes.
    ''' </summary>
    ''' <param name="n">The node.</param>
    ''' <param name="q">The queue.</param>
    Private Sub SetNodeExpansionState(ByVal n As TreeNode, ByRef q As Queue(Of Boolean))
        If n.Nodes.Count > 0 AndAlso q.Count > 0 AndAlso q.Dequeue = True Then n.Expand()

        For Each no As TreeNode In n.Nodes
            SetNodeExpansionState(no, q)
        Next
    End Sub



I experimented with arrays, stacks, and lists as well. The queue seemed to be a perfect fit for my purposes. One thing to keep in mind is that this will only work if the number of nodes between the trees stays the same. If the number of nodes changes, then the position in the queue is going to change and you will get unexpected results.

As always, i hope someone out there finds this useful. I know i do. If you find inaccuracies, have more questions, or find it helpful, please leave a comment.

0 Comments On This Entry

 

Trackbacks for this entry [ Trackback URL ]

There are no Trackbacks for this entry

July 2014

S M T W T F S
  12345
6789101112
13141516171819
20212223 24 2526
2728293031  

Recent Entries

Search My Blog

0 user(s) viewing

0 Guests
0 member(s)
0 anonymous member(s)

Categories