TreeNodeCollection.Contains(TreeNode) 方法

定義

判斷指定的樹狀節點是否為集合的成員。

public:
 bool Contains(System::Windows::Forms::TreeNode ^ node);
public bool Contains (System.Windows.Forms.TreeNode node);
member this.Contains : System.Windows.Forms.TreeNode -> bool
Public Function Contains (node As TreeNode) As Boolean

參數

node
TreeNode

要在集合中尋找的 TreeNode

傳回

Boolean

如果 TreeNode 是集合的成員,則為 true,否則為 false

範例

下列程式碼範例會判斷指定的 TreeNode 是否在 內 TreeNodeCollection ,然後列舉集合。 此範例需要您具有 Form TreeView ,且具有 TreeNodeCollection TreeNode 包含具名 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

備註

這個方法可讓您先判斷 是否 TreeNode 為 集合的成員,再嘗試在 上 TreeNode 執行作業。 您可以使用這個方法來確認 TreeNode 已新增至 或 仍然是集合的成員。

此方法所花費的時間量與節點集合的大小成正比,因此您可以避免搭配大型集合使用。

這個方法只會檢查參考是否相等。 您無法使用它來判斷是否相等但不同的節點位於集合中。

注意

參考相等需求的其中一個含意是,您無法覆寫 Equals 類別的 TreeNode 方法,自訂這個方法 TreeNode 的行為。

適用於