Bahasa

TreeNodeCollection.Contains(TreeNode) Metode

Definisi

Menentukan apakah simpul pohon yang ditentukan adalah anggota koleksi.

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

Parameter

node
TreeNode

untuk TreeNode menemukan dalam koleksi.

Mengembalikan

true TreeNode jika adalah anggota koleksi; jika tidak, false.

Contoh

Contoh kode berikut menentukan apakah yang ditentukan TreeNode berada dalam TreeNodeCollection, lalu menghitung koleksi. Contoh ini mengharuskan Anda memiliki dengan yang memiliki Form yang berisi bernama TreeViewTreeNodeCollection .TreeNodemyTreeNode2

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

Keterangan

Metode ini memungkinkan Anda menentukan apakah adalah TreeNode anggota koleksi sebelum mencoba melakukan operasi pada TreeNode. Anda dapat menggunakan metode ini untuk mengonfirmasi bahwa TreeNode telah ditambahkan atau masih menjadi anggota koleksi.

Jumlah waktu yang dibutuhkan metode ini sebanding dengan ukuran koleksi simpul, jadi Anda mungkin ingin menghindari penggunaannya dengan koleksi besar.

Metode ini hanya memeriksa kesetaraan referensi. Anda tidak dapat menggunakannya untuk menentukan apakah simpul yang setara tetapi berbeda ada dalam koleksi.

Note

Salah satu implikasi dari persyaratan kesetaraan referensi adalah Anda tidak dapat menyesuaikan perilaku metode ini untuk jenis turunan TreeNodeEquals dengan mengambil alih metode TreeNode kelas.

Berlaku untuk