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

Remarks

This method enables you to determine whether a TreeNode is a member of the collection before attempting to perform operations on the TreeNode. You can use this method to confirm that a TreeNode has been added to or is still a member of the collection.

The amount of time this method takes is proportional to the size of the node collection, so you may want to avoid using it with large collections.

This method checks for reference equality only. You cannot use it to determine whether an equivalent but different node is in the collection.

Note

One implication of the reference-equality requirement is that you cannot customize the behavior of this method for derived TreeNode types by overriding the Equals method of the TreeNode class.