TreeNodeCollection.IndexOf(TreeNode) Metodo

Definizione

Restituisce l'indice del nodo della struttura ad albero specificato nell'insieme.

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

Parametri

node
TreeNode

Oggetto TreeNode da individuare nella raccolta.

Restituisce

Int32

Indice in base zero dell'elemento trovato nell'insieme dei nodi della struttura ad albero; in caso contrario, -1.

Esempio

Nell'esempio di codice seguente viene determinato se un oggetto specificato TreeNode si trova all'interno di un TreeNodeCollectionoggetto e quindi enumera la raccolta. In questo esempio è necessario disporre di un Form oggetto con un TreeView oggetto che TreeNodeCollection contiene un TreeNode oggetto denominato 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

Commenti

Il tempo impiegato da questo metodo è proporzionale alle dimensioni della raccolta di nodi, pertanto è consigliabile evitare di usarlo con raccolte di grandi dimensioni.

Questo metodo controlla solo l'uguaglianza dei riferimenti. Non è possibile usarlo per recuperare l'indice di un nodo equivalente ma diverso nella raccolta.

Nota

Un'implicazione del requisito di uguaglianza dei riferimenti è che non è possibile personalizzare il comportamento di questo metodo per i tipi derivati TreeNode eseguendo l'override del Equals metodo della TreeNode classe .

Si applica a