Save TreeView State

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 of a TreeView and save whether the node was expanded or collapsed. Notice that to keep things simple, the source code only saves the state of top-level nodes:

 

Code

 

Load TreeView State

Once the TreeView state is saved, loading the state is just as simple:

 

Code

The C# functions Expand and Collapse make it simple to set the previous state of the TreeView nodes.

posted @ 2009-01-22 15:28  海洋——海纳百川,有容乃大.  阅读(224)  评论(0编辑  收藏  举报