So for example, let's say you have a C # program that builds a Treeview programmatically. Since elements are dynamic, the Treeview is cleared and repopulated every time a single node is changed.
Save Treeview state
The concept is simple: iterate through the top level nodes ofTreeviewAnd save whether the node was expanded or collapsed. Notice that to keep things simple, the source code onlySaves the stateOf top-level nodes:
Code
Private Dictionary < String , Bool > Savetreestate (Treeview tree)
{
Dictionary < String , Bool > Nodestates = New Dictionary < String , Bool > ();
For ( Int I = 0 ; I < Tree. nodes. Count; I ++ )
{
If (Tree. nodes [I]. nodes. Count > 0 )
{
Nodestates. Add (tree. nodes [I]. Name, tree. nodes [I]. isexpanded );
}
}
ReturnNodestates;
}
Load Treeview state
OnceTreeviewState is saved,Loading the stateIs just as simple:
Code
Private Void Restoretreestate (Treeview tree, Dictionary < String , Bool > Treestate)
{
For ( Int I = 0 ; I < Tree. nodes. Count; I ++ )
{
If (Treestate. containskey (tree. nodes [I]. Name ))
{
If (Treestate [tree. nodes [I]. Name])
Tree. nodes [I]. Expand ();
Else
Tree. nodes [I]. Collapse ();
}
}
}
the C # functions Expand and collapse make it simple to set the previous state of the Treeview nodes.