This documentation is archived and is not being maintained.
TreeNodeCollection.IndexOf Method
Visual Studio 2010
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 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.
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(); while(myEnumerator.MoveNext()) { myLabel.Text += ((TreeNode)myEnumerator.Current).Text +"\n"; } }
Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Show: