Skip to content

Commit 8b91e6c

Browse files
authored
Merge pull request #2 from SyncfusionExamples/SweathaBharathi-patch-1
Update README.md
2 parents 988d34a + a98acce commit 8b91e6c

File tree

1 file changed

+95
-1
lines changed

1 file changed

+95
-1
lines changed

README.md

Lines changed: 95 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,96 @@
11
# How to save and load the tree node in a collapsed or an expanded state in WinForms TreeViewAdv?
2-
This example illustrates how to save and load the tree node in a collapsed or an expanded state in WinForms TreeViewAdv.
2+
3+
In [WinForms TreeViewAdv](https://www.syncfusion.com/winforms-ui-controls/treeview) does not have support for saving its TreeNodeAdvCollection in an expanded or a collapsed state. This requirement can be achieved by the following steps.​
4+
5+
1. Save the Expanded or Collapsed state of each of its TreeNodeAdvCollection to a Dictionary.
6+
2. Reload the saved state based on user requirement.
7+
8+
**C#**
9+
10+
```csharp
11+
//Initializes the collection
12+
Dictionary<string, bool> dic = new Dictionary<string, bool>();
13+
14+
//Saves the Tree State
15+
dic = SaveTreeState(this.treeViewAdv1);
16+
17+
//Loads the Tree state
18+
this.RestoreTreeState(this.treeViewAdv1, dic);
19+
20+
//Saves TreeViewAdv
21+
public Dictionary<string, bool> SaveTreeState(TreeViewAdv tree)
22+
{
23+
Dictionary<string, bool> nodeStates = new Dictionary<string, bool>();
24+
foreach (TreeNodeAdv node in Collect(tree.Nodes))
25+
{
26+
nodeStates.Add(node.Text,node.Expanded);
27+
}
28+
return nodeStates;
29+
}
30+
31+
//Loads TreeViewAdv
32+
private void RestoreTreeState(TreeViewAdv tree, Dictionary<string, bool> treeState)
33+
{
34+
foreach (TreeNodeAdv node in Collect(tree.Nodes))
35+
{
36+
if (treeState.Keys.Count != 0 && treeState[node.Text])
37+
node.Expand();
38+
else
39+
node.Expanded = false;
40+
}
41+
}
42+
43+
//Iterates through all the nodes in TreeViewAdv
44+
IEnumerable<TreeNodeAdv> Collect(TreeNodeAdvCollection nodes)
45+
{
46+
foreach(TreeNodeAdv node in nodes)
47+
{
48+
yield return node;
49+
foreach (TreeNodeAdv child in Collect(node.Nodes))
50+
yield return child;
51+
}
52+
}
53+
```
54+
55+
**VB.Net**
56+
57+
```vbnet
58+
'Initializes the collection
59+
Private dic As New Dictionary(Of String, Boolean)()
60+
61+
'Saves the Tree State
62+
dic = SaveTreeState(Me.treeViewAdv1)
63+
64+
'Loads the Tree state
65+
Me.RestoreTreeState(Me.treeViewAdv1, dic)
66+
67+
'Saves TreeViewAdv.
68+
Public Function SaveTreeState(ByVal tree As TreeViewAdv) As Dictionary(Of String, Boolean)
69+
Dim nodeStates As New Dictionary(Of String, Boolean)()
70+
For Each node As TreeNodeAdv In Collect(tree.Nodes)
71+
nodeStates.Add(node.Text, node.Expanded)
72+
Next node
73+
Return nodeStates
74+
End Function
75+
76+
'Loads TreeViewAdv
77+
Private Sub RestoreTreeState(ByVal tree As TreeViewAdv, ByVal treeState As Dictionary(Of String, Boolean))
78+
For Each node As TreeNodeAdv In Collect(tree.Nodes)
79+
If treeState.Keys.Count <> 0 AndAlso treeState(node.Text) Then
80+
node.Expand()
81+
Else
82+
node.Expanded = False
83+
End If
84+
Next node
85+
End Sub
86+
87+
'Iterates through all the nodes in TreeViewAdv
88+
Private Function Collect(ByVal nodes As TreeNodeAdvCollection) As IEnumerable(Of TreeNodeAdv)
89+
For Each node As TreeNodeAdv In nodes
90+
Return node
91+
For Each child As TreeNodeAdv In Collect(node.Nodes)
92+
Return child
93+
Next child
94+
Next node
95+
End Function
96+
```

0 commit comments

Comments
 (0)