This documentation is archived and is not being maintained.

TreeNodeCollection.GetEnumerator Method

Returns an enumerator that can be used to iterate through the tree node collection.

[Visual Basic]
Public Overridable Function GetEnumerator() As IEnumerator _
   Implements IEnumerable.GetEnumerator
public virtual IEnumerator GetEnumerator();
public: virtual IEnumerator* GetEnumerator();
public function GetEnumerator() : IEnumerator;

Return Value

An IEnumerator object that represents the tree node collection.




[Visual Basic, C#, C++] The following example determines if a specified TreeNode is within a TreeNodeCollection and enumerates the collection. This example assumes you have a Form with a TreeView that has a TreeNodeCollection that contains a TreeNode named myTreeNode2.

[Visual Basic] 
Private Sub EnumerateTreeNodes()
   Dim myNodeCollection As TreeNodeCollection = myTreeView.Nodes
   ' Check for a node in the collection.
   If myNodeCollection.Contains(myTreeNode2) Then
      myLabel.Text += "Node2 is at index: " + myNodeCollection.IndexOf(myTreeNode2)
   End If
   myLabel.Text += ControlChars.Cr + ControlChars.Cr + _
     "Elements of the TreeNodeCollection:" + ControlChars.Cr
   ' Create an enumerator for the collection.
   Dim myEnumerator As IEnumerator = myNodeCollection.GetEnumerator()
   While myEnumerator.MoveNext()
      myLabel.Text += CType(myEnumerator.Current, TreeNode).Text + ControlChars.Cr
   End While
End Sub 

private void EnumerateTreeNodes()
   TreeNodeCollection myNodeCollection = myTreeView.Nodes;
   // Check for a node in the collection.
   if (myNodeCollection.Contains(myTreeNode2))
      myLabel.Text += "Node2 is at index: " + myNodeCollection.IndexOf(myTreeNode2);
   myLabel.Text += "\n\nElements of the TreeNodeCollection:\n";

   // Create an enumerator for the collection.
   IEnumerator myEnumerator = myNodeCollection.GetEnumerator();
      myLabel.Text += ((TreeNode)myEnumerator.Current).Text +"\n";

    void EnumerateTreeNodes() {
        TreeNodeCollection* myNodeCollection = myTreeView->Nodes;
        // Check for a node in the collection.
        if (myNodeCollection->Contains(myTreeNode2)) {
            myLabel->Text = String::Concat(myLabel->Text, 
                S"Node2 is at index: ", 
        myLabel->Text = String::Concat(myLabel->Text, 
                S"\n\nElements of the TreeNodeCollection:\n");

        // Create an enumerator for the collection.
        IEnumerator* myEnumerator = myNodeCollection->GetEnumerator();
        while(myEnumerator->MoveNext()) {
            myLabel->Text = String::Concat(myLabel->Text, 
                myEnumerator->Current))->Text, S"\n");

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.


Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework

See Also

TreeNodeCollection Class | TreeNodeCollection Members | System.Windows.Forms Namespace