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
参数
返回
示例
下面的代码示例确定指定 TreeNode 是否在集合 TreeNodeCollection中,然后枚举集合。 此示例要求你有一个FormTreeView包含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集合执行操作之前确定是否TreeNode是集合的成员。 可以使用此方法确认 TreeNode 已添加到集合中还是仍然是集合的成员。
此方法花费的时间量与节点集合的大小成正比,因此你可能希望避免将它与大型集合一起使用。
此方法仅检查引用相等性。 不能使用它来确定集合中是否具有等效但不同的节点。