How to save and load the TreeNodeAdv in a collapsed or an expanded state in WF TreeViewAdv?
Save and load the TreeNodeAdv
The WinForms 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
Figure 1: Tree Node state of the TreeViewAdv can be saved or loaded by choosing the Save Tree State and Load Tree State
You can download the example from GitHub
​Conclusion
I hope you enjoyed learning about how to save and load the TreeNodeAdv in a collapsed or an expanded state in WinForms TreeViewAdv.
You can refer to our WinForms TreeViewAdv’s feature tour page to know about its other groundbreaking feature representations. You can also explore our WinForms TreeViewAdv documentation to understand how to present and manipulate data.
For current customers, you can check out WinForms components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our WinForms TreeViewAdv and other WinForms components.
If you have any queries or require clarifications, please let us know in comments below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!