Export (0) Print
Expand All

TreeNodeCollection.Contains Method

Determines whether the specified tree node is a member of the collection.

Namespace:  System.Windows.Forms
Assembly:  System.Windows.Forms (in System.Windows.Forms.dll)

public bool Contains(
	TreeNode node
)

Parameters

node
Type: System.Windows.Forms.TreeNode

The TreeNode to locate in the collection.

Return Value

Type: System.Boolean
true if the TreeNode is a member of the collection; otherwise, false.

This method enables you to determine whether a TreeNode is a member of the collection before attempting to perform operations on the TreeNode. You can use this method to confirm that a TreeNode has been added to or is still a member of the collection.

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 determine whether an equivalent but different node is in the collection.

NoteNote

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.

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.

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";
   }
}

.NET Framework

Supported in: 4.5.2, 4.5.1, 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Show:
© 2014 Microsoft