TreeNodeCollection.Contains(TreeNode) 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
判斷指定的樹節點是否屬於集合。
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
參數
傳回
true 若 是 TreeNode 集合的成員;否則, false。
範例
以下程式碼範例判斷指定的 TreeNode 是否屬於 TreeNodeCollection,然後枚舉該集合。 此範例要求你有一個 Form ,TreeView且 有 ,且 包含TreeNodeCollectionTreeNode一個名為 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前,判斷 a TreeNode 是否屬於集合。 你可以用這個方法確認 a TreeNode 是否已被加入或仍然是該集合的成員。
這個方法所需的時間與節點集合的大小成正比,因此你可能想避免在大型集合中使用它。
此方法僅檢查參考資料的相等性。 你無法用它來判斷集合中是否有相同但不同的節點。