TreeNodeCollection.IndexOf(TreeNode) Método

Definición

Devuelve el índice del nodo de árbol especificado de la colección.

public:
 int IndexOf(System::Windows::Forms::TreeNode ^ node);
public int IndexOf (System.Windows.Forms.TreeNode node);
member this.IndexOf : System.Windows.Forms.TreeNode -> int
Public Function IndexOf (node As TreeNode) As Integer

Parámetros

node
TreeNode

TreeNode que se va a buscar en la colección.

Devoluciones

Int32

Índice de base cero del elemento encontrado en la colección de nodos de árbol; en caso contrario, -1.

Ejemplos

En el ejemplo de código siguiente se determina si un especificado TreeNode está dentro de y TreeNodeCollection, a continuación, enumera la colección . En este ejemplo se requiere que tenga un Form con un TreeView que tenga un TreeNodeCollection que contenga un TreeNode elemento denominado 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";
   }
}
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";
   }
}
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

Comentarios

La cantidad de tiempo que tarda este método es proporcional al tamaño de la colección de nodos, por lo que es posible que quiera evitar su uso con colecciones grandes.

Este método solo comprueba la igualdad de referencia. No se puede usar para recuperar el índice de un nodo equivalente pero diferente de la colección.

Nota

Una implicación del requisito de igualdad de referencia es que no se puede personalizar el comportamiento de este método para los tipos derivados TreeNode invalidando el Equals método de la TreeNode clase .

Se aplica a