Export (0) Print
Expand All

TreeNodeCollection.Contains Method

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

[Visual Basic]
Public Function Contains( _
   ByVal node As TreeNode _
) As Boolean
[C#]
public bool Contains(
 TreeNode node
);
[C++]
public: bool Contains(
 TreeNode* node
);
[JScript]
public function Contains(
   node : TreeNode
) : Boolean;

Parameters

node
The TreeNode to locate in the collection.

Return Value

true if the TreeNode is a member of the collection; otherwise, false.

Remarks

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.

Example

[Visual Basic, C#, C++] The following example determines if a specified TreeNode is within a TreeNodeCollection and enumerates the collection. This example assumes you have a Form with a TreeView that has a TreeNodeCollection that contains a TreeNode named myTreeNode2.

[Visual Basic] 
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 

[C#] 
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";
   }
}

[C++] 
private:
    void EnumerateTreeNodes() {
        TreeNodeCollection* myNodeCollection = myTreeView->Nodes;
        // Check for a node in the collection.
        if (myNodeCollection->Contains(myTreeNode2)) {
            myLabel->Text = String::Concat(myLabel->Text, 
                S"Node2 is at index: ", 
                __box(myNodeCollection->IndexOf(myTreeNode2)));
        }
        myLabel->Text = String::Concat(myLabel->Text, 
                S"\n\nElements of the TreeNodeCollection:\n");

        // Create an enumerator for the collection.
        IEnumerator* myEnumerator = myNodeCollection->GetEnumerator();
        while(myEnumerator->MoveNext()) {
            myLabel->Text = String::Concat(myLabel->Text, 
                (dynamic_cast<TreeNode*>(
                myEnumerator->Current))->Text, S"\n");
        }
    }

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework

See Also

TreeNodeCollection Class | TreeNodeCollection Members | System.Windows.Forms Namespace

Show:
© 2014 Microsoft