Share via


TreeNodeCollection.IndexOf(TreeNode) Méthode

Définition

Retourne l'index du nœud d'arbre spécifié dans la collection.

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

Paramètres

node
TreeNode

TreeNode à rechercher dans la collection.

Retours

Int32

Index de base zéro de l'élément trouvé dans la collection de nœuds d'arbre ; sinon, -1.

Exemples

L’exemple de code suivant détermine si un élément spécifié TreeNode se trouve dans un TreeNodeCollection, puis énumère la collection. Cet exemple nécessite que vous ayez un Form avec un TreeView qui contient un TreeNodeCollection TreeNode nommé 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

Remarques

Le temps nécessaire à cette méthode est proportionnel à la taille de la collection de nœuds. Vous pouvez donc éviter de l’utiliser avec de grandes collections.

Cette méthode vérifie uniquement l’égalité des références. Vous ne pouvez pas l’utiliser pour récupérer l’index d’un nœud équivalent mais différent dans la collection.

Notes

L’une des implications de l’exigence d’égalité de référence est que vous ne pouvez pas personnaliser le comportement de cette méthode pour les types dérivés TreeNode en remplaçant la Equals méthode de la TreeNode classe.

S’applique à