TreeNodeEnumerator Class

A type-safe enumerator for tree view controls that can be used to enumerate all of its nodes recursively or one branch of it.

Definition

Namespace: EWSoftware.ListControls
Assembly: EWSoftware.ListControls (in EWSoftware.ListControls.dll) Version: 2023.4.9.0
public class TreeNodeEnumerator : IEnumerator
Inheritance
Object    TreeNodeEnumerator
Implements
IEnumerator

Remarks

In addition to the ExtendedTreeView, this can be used manually to enumerate the nodes in a standard TreeView control as well.

Example

C# - Enumerate the entire tree
txtEnumResults.Text = null;

// Use foreach() on the ExtendedTreeView control itself to
// enumerate all of its nodes recursively.
foreach(TreeNode node in tvExtTree)
    txtEnumResults.AppendText($"{new String(' ', node.Level * 4)}{node.Text}\r\n");
VB.NET - Enumerate the entire tree
Dim node As TreeNode
txtEnumResults.Text = Nothing

' Use For Each on the ExtendedTreeView control itself to enumerate all of its nodes recursively
For Each node in tvExtTree
    txtEnumResults.AppendText($"{New String(" "C, node.Level * 4)}{node.Text}" & Environment.NewLine)
Next
C# - Enumerate starting at a selected node
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($"Manual Enum: {new String(' ', node.Level * 4)}{node.Text}\r\n");
}

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($"Enum Helper: {new String(' ', tn.Level * 4)}{tn.Text}\r\n");
VB.NET - Enumerate starting at a selected node
Dim enumerateSiblings As Boolean = False
Dim node As TreeNode
Dim startNode As TreeNode = tvExtTree.SelectedNode

If sender Is btnEnumNodeSibs Then
    enumerateSiblings = True
End If

If startNode Is Nothing Then
    txtEnumResults.Text = "Select a starting node first"
    Return
End If

txtEnumResults.Text = Nothing

' 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.
Dim enumerator As New TreeNodeEnumerator(startNode, enumerateSiblings)

' Call the MoveNext() method to move through each node.  Use the Current
' property to access the current node.
Do While enumerator.MoveNext()
    node = enumerator.Current

    txtEnumResults.AppendText($"Manual Enum: {New String(" "C, node.Level * 4)}{node.Text}" &
        Environment.NewLine)
Loop

txtEnumResults.AppendText(Environment.NewLine & Environment.NewLine)

' We can also use the helper method to simplify it
For Each node In TreeNodeEnumerator.Enumerate(startNode, enumerateSiblings)
    txtEnumResults.AppendText($"Enum Helper: {New String(" "C, node.Level * 4)}{node.Text}" &
        Environment.NewLine)
Next

Constructors

TreeNodeEnumerator Constructor

Properties

Current Type-safe enumerator Current method

Methods

Enumerate This method can be used to enumerate tree nodes in a more convenient way using a foreach loop without having to manually construct and manage the enumerator.
EqualsDetermines whether the specified object is equal to the current object.
(Inherited from Object)
FinalizeAllows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection.
(Inherited from Object)
GetHashCodeServes as the default hash function.
(Inherited from Object)
GetTypeGets the Type of the current instance.
(Inherited from Object)
MemberwiseCloneCreates a shallow copy of the current Object.
(Inherited from Object)
MoveNext Move to the next element
Reset Reset the enumerator to the start
ToStringReturns a string that represents the current object.
(Inherited from Object)

See Also