Пример #1
0
        /// <summary>
        /// Gets the state of the treeview nodes.
        /// Use with ApplyOldStateInfo to restore state after repopulating controll.
        /// </summary>
        /// <param name="tv"></param>
        /// <returns></returns>
        public static TreeViewState GetState(this TreeView tv)
        {
            TreeViewState state = new TreeViewState();

            //initial list of nodes
            List <TreeNode> nodes = new List <TreeNode>();

            foreach (TreeNode node in tv.Nodes)
            {
                nodes.Add(node);
            }

            //interate all nodes
            while (nodes.Count > 0)
            {
                TreeNode node = nodes[0];
                nodes.RemoveAt(0);

                //add state to results
                TreeNodePath path = new TreeNodePath(node);
                TreeViewState.TreeNodeState tns = new TreeViewState.TreeNodeState(node);
                if (!state.nodes.ContainsKey(path))
                {
                    state.nodes.Add(path, tns);
                }
                else
                {
                    WDAppLog.logError(ErrorLevel.SmallError, "tree view has two identical node pathways");
                    continue; //loop back rather then examine children, so as to stop any infinite loops.
                }


                //add child nodes to list
                foreach (TreeNode childNode in node.Nodes)
                {
                    nodes.Add(childNode);
                }
            }

            //store selected node
            state.selectedNode = GetPath(tv.SelectedNode);

            return(state);
        }
Пример #2
0
        /// <summary>
        /// Uses existing state info to relayout a treeview, that was assumably repopulated.
        /// Is fairly tolerant of nodes that are new or missing.
        /// Not accurate when notes .Text feild changes
        /// Uses .Text not .Name
        /// </summary>
        /// <param name="tv"></param>
        /// <param name="oldState"></param>
        /// <param name="restoreExpansion"></param>
        /// <param name="restoreCheckBox"></param>
        /// <param name="restoreSelection"></param>
        public static void ApplyOldStateInfo(this TreeView tv,
                                             TreeViewState oldState,
                                             bool restoreExpansion,
                                             bool restoreCheckBox,
                                             bool restoreSelection)
        {
            //initial list of nodes
            List <TreeNode> nodes = new List <TreeNode>();

            foreach (TreeNode node in tv.Nodes)
            {
                nodes.Add(node);
            }

            //interate all nodes
            while (nodes.Count > 0)
            {
                TreeNode node = nodes[0];
                nodes.RemoveAt(0);

                //restore old state
                TreeNodePath path = new TreeNodePath(node);
                if (oldState.nodes.ContainsKey(path))
                {
                    TreeViewState.TreeNodeState tns = oldState.nodes[path];

                    if (restoreExpansion)
                    {
                        if (tns.isExpanded && (!node.IsExpanded))
                        {
                            node.Expand();
                        }
                        else if ((!tns.isExpanded) && node.IsExpanded)
                        {
                            node.Collapse(true);
                        }
                    }

                    if (restoreCheckBox)
                    {
                        node.Checked = tns.isChecked;
                    }
                }

                //add child nodes to list
                foreach (TreeNode childNode in node.Nodes)
                {
                    nodes.Add(childNode);
                }
            }

            //restore selected node
            if (restoreSelection)
            {
                TreeNode selNode = GetNode(tv, oldState.selectedNode);
                if (selNode != null)
                {
                    tv.SelectedNode = selNode;
                }
            }
        }