TreeNodeCollection.IndexOf(TreeNode) Method
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Returns the index of the specified tree node in the 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
The 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
.
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
The amount of time this method takes is proportional to the size of the node collection, so you may want to avoid using it with large collections.
This method checks for reference equality only. You cannot use it to retrieve the index of an equivalent but different node in the collection.
Notes
One implication of the reference-equality requirement is that you cannot customize the behavior of this method for derived TreeNode types by overriding the Equals method of the TreeNode class.
Produit | Versions |
---|---|
.NET Framework | 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1 |
Windows Desktop | 3.0, 3.1, 5, 6, 7, 8, 9 |
Commentaires sur .NET
.NET est un projet open source. Sélectionnez un lien pour fournir des commentaires :