)
We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. (Last updated on: June 24, 2019).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

How to save and load the TreeNodeAdv in a collapsed or an expanded state in WinForms TreeViewAdv?

Platform: WinForms |
Control: TreeViewAdv |
Published Date: August 18, 2015 |
Last Revised Date: February 4, 2020

Save and load the TreeNodeAdv

The TreeViewAdv does not have support for saving its TreeNodeAdvCollection in an expanded or a collapsed state. This requirement can be achieved by the following steps.

1. Save the Expanded or Collapsed state of each of its TreeNodeAdvCollection to a Dictionary.

2. Reload the saved state based on user requirement.

C#

//Initializes the collection
Dictionary<string, bool> dic = new Dictionary<string, bool>();
//Saves the Tree State
dic = SaveTreeState(this.treeViewAdv1);
//Loads the Tree state
this.RestoreTreeState(this.treeViewAdv1, dic);
//Saves TreeViewAdv
public Dictionary<string, bool> SaveTreeState(TreeViewAdv tree)
{
    Dictionary<string, bool> nodeStates = new Dictionary<string, bool>();  
    foreach (TreeNodeAdv node in Collect(tree.Nodes))
    {
        nodeStates.Add(node.Text,node.Expanded);
    }
    return nodeStates;
}
//Loads TreeViewAdv
private void RestoreTreeState(TreeViewAdv tree, Dictionary<string, bool> treeState)
{
    foreach (TreeNodeAdv node in Collect(tree.Nodes))
    {
        if (treeState.Keys.Count != 0 && treeState[node.Text])
            node.Expand();
        else
            node.Expanded = false;
    }
}
//Iterates through all the nodes in TreeViewAdv
IEnumerable<TreeNodeAdv> Collect(TreeNodeAdvCollection nodes)
{
    foreach(TreeNodeAdv node in nodes)
    {
        yield return node;
        foreach (TreeNodeAdv child in Collect(node.Nodes))
        yield return child;
    }
}

VB

'Initializes the collection
Private dic As New Dictionary(Of String, Boolean)()
'Saves the Tree State
dic = SaveTreeState(Me.treeViewAdv1)
'Loads the Tree state
Me.RestoreTreeState(Me.treeViewAdv1, dic)
'Saves TreeViewAdv.
Public Function SaveTreeState(ByVal tree As TreeViewAdv) As Dictionary(Of String, Boolean)
    Dim nodeStates As New Dictionary(Of String, Boolean)()
    For Each node As TreeNodeAdv In Collect(tree.Nodes)
        nodeStates.Add(node.Text, node.Expanded)
    Next node
    Return nodeStates
End Function
'Loads TreeViewAdv
Private Sub RestoreTreeState(ByVal tree As TreeViewAdv, ByVal treeState As Dictionary(Of String, Boolean))
    For Each node As TreeNodeAdv In Collect(tree.Nodes)
        If treeState.Keys.Count <> 0 AndAlso treeState(node.Text) Then
 node.Expand()
        Else
 node.Expanded = False
        End If
    Next node
End Sub
'Iterates through all the nodes in TreeViewAdv
Private Function Collect(ByVal nodes As TreeNodeAdvCollection) As IEnumerable(Of TreeNodeAdv)
   For Each node As TreeNodeAdv In nodes
       Return node
       For Each child As TreeNodeAdv In Collect(node.Nodes)
       Return child
       Next child
   Next node
End Function

Show the load and save tree state

Figure 1: Tree Node state of the TreeViewAdv can be saved or loaded by choosing the Save Tree State and Load Tree State

Samples:

C#: http://www.syncfusion.com/downloads/support/directtrac/142201/ze/TreeNodeAdvState-1931243813

VB: http://www.syncfusion.com/downloads/support/directtrac/142201/ze/TreeNodeAdvState_VB-2113180951

2X faster development

The ultimate WinForms UI toolkit to boost your development speed.
ADD COMMENT
You must log in to leave a comment

Please sign in to access our KB

This page will automatically be redirected to the sign-in page in 10 seconds.

Up arrow icon

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile
Live Chat Icon