TreeNodeCollection::IndexOf Method (TreeNode^)
Returns the index of the specified tree node in the collection.
Assembly: System.Windows.Forms (in System.Windows.Forms.dll)
Parameters
- node
-
Type:
System.Windows.Forms::TreeNode^
The TreeNode to locate in the collection.
Return Value
Type: System::Int32The zero-based index of the item found in the tree node collection; otherwise, -1.
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 retrieve the index of an equivalent but different node in the collection.
The following code example determines if a specified TreeNode is within a TreeNodeCollection, and then enumerates the collection. This example requires that you have a Form with a TreeView that has a TreeNodeCollection that contains a TreeNode named myTreeNode2.
void EnumerateTreeNodes() { TreeNodeCollection^ myNodeCollection = myTreeView->Nodes; // Check for a node in the collection. if ( myNodeCollection->Contains( myTreeNode2 ) ) { myLabel->Text = myLabel->Text + "Node2 is at index: " + myNodeCollection->IndexOf( myTreeNode2 ); } myLabel->Text = myLabel->Text + "\n\nElements of the TreeNodeCollection:\n"; // Create an enumerator for the collection. IEnumerator^ myEnumerator = myNodeCollection->GetEnumerator(); while ( myEnumerator->MoveNext() ) { myLabel->Text = myLabel->Text + (dynamic_cast<TreeNode^>(myEnumerator->Current))->Text + "\n"; } }
Available since 1.1
