Beispiel #1
0
        //=====================================================================

        /// <summary>
        /// This method can be used to enumerate tree nodes in a more convenient way using a <c>foreach</c>
        /// loop without having to manually construct and manage the enumerator.
        /// </summary>
        /// <param name="start">The node at which to start enumeration.</param>
        /// <param name="enumerateSiblings">True to enumerate the starting node's siblings as well or false to
        /// stop after enumerating the starting node and all of its children.</param>
        /// <returns>An enumerable list of tree nodes</returns>
        public static IEnumerable <TreeNode> Enumerate(TreeNode start, bool enumerateSiblings)
        {
            var tne = new TreeNodeEnumerator(start, enumerateSiblings);

            while (tne.MoveNext())
            {
                yield return(tne.Current);
            }
        }
        /// <summary>
        /// Enumerate just the selected node (branch) and, based on the sender, the subsequent siblings
        /// </summary>
        /// <param name="sender">The sender of the event</param>
        /// <param name="e">The event arguments</param>
        private void btnEnumNode_Click(object sender, EventArgs e)
        {
            #region TreeNodeEnumerator Example
            bool enumerateSiblings = (sender == btnEnumNodeSibs);
            TreeNode node, startNode = tvExtTree.SelectedNode;

            if(startNode == null)
            {
                txtEnumResults.Text = "Select a starting node first";
                return;
            }

            txtEnumResults.Text = null;

            // For this, we create the enumerator manually and pass it
            // the starting node and a flag indicating whether or not
            // to enumerate the siblings of the starting node as well.
            TreeNodeEnumerator enumerator = new TreeNodeEnumerator(startNode,
                enumerateSiblings);

            // Call the MoveNext() method to move through each node.  Use the
            // Current property to access the current node.
            while(enumerator.MoveNext())
            {
                node = enumerator.Current;

                txtEnumResults.AppendText(String.Format("Manual Enum: {0}{1}\r\n",
                    new String(' ', node.Level * 4), node.Text));
            }

            txtEnumResults.AppendText("\r\n\r\n");

            // We can also use the helper method to simplify it
            foreach(TreeNode tn in TreeNodeEnumerator.Enumerate(startNode, enumerateSiblings))
                txtEnumResults.AppendText(String.Format("Enum Helper: {0}{1}\r\n",
                    new String(' ', tn.Level * 4), tn.Text));
            #endregion
        }
        //=====================================================================
        /// <summary>
        /// This method can be used to enumerate tree nodes in a more convenient way using a <c>foreach</c>
        /// loop without having to manually construct and manage the enumerator.
        /// </summary>
        /// <param name="start">The node at which to start enumeration.</param>
        /// <param name="enumerateSiblings">True to enumerate the starting node's siblings as well or false to
        /// stop after enumerating the starting node and all of its children.</param>
        /// <returns>An enumerable list of tree nodes</returns>
        public static IEnumerable<TreeNode> Enumerate(TreeNode start, bool enumerateSiblings)
        {
            var tne = new TreeNodeEnumerator(start, enumerateSiblings);

            while(tne.MoveNext())
                yield return tne.Current;
        }