Save Treeview state

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.