TreeNodeCollection.IndexOf(TreeNode) Método

Definição

Devolve o índice do nó de árvore especificado na coleção.

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

A TreeNode localizar na coleção.

Devoluções

O índice baseado em zero do item encontrado na coleção de nós da árvore; caso contrário, -1.

Exemplos

O exemplo de código seguinte determina se um especificado TreeNode pertence a um TreeNodeCollection, e depois enumera a coleção. Este exemplo exige que tenhas um Form com um TreeView que tem um TreeNodeCollection que contém um TreeNode nome 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

Observações

O tempo que este método demora é proporcional ao tamanho da coleção de nós, por isso pode querer evitar usá-lo com coleções grandes.

Este método verifica apenas a igualdade de referência. Não pode usá-lo para recuperar o índice de um nó equivalente mas diferente na coleção.

Note

Uma implicação do requisito de igualdade de referência é que não se pode personalizar o comportamento deste método para tipos derivados TreeNode sobrescrevendo o Equals método da TreeNode classe.

Aplica-se a