Share via


TreeNodeCollection.Count Özellik

Tanım

Koleksiyondaki toplam nesne sayısını TreeNode alır.

public:
 property int Count { int get(); };
[System.ComponentModel.Browsable(false)]
public int Count { get; }
[<System.ComponentModel.Browsable(false)>]
member this.Count : int
Public ReadOnly Property Count As Integer

Özellik Değeri

Koleksiyondaki toplam nesne sayısı TreeNode .

Uygulamalar

Öznitelikler

Örnekler

Aşağıdaki kod örneği içindeki TreeNodeCollectionnesne sayısını TreeNode görüntüler, koleksiyonun içeriğini bir Object diziye kopyalar ve denetimdeki Label ağaç düğümlerinin listesini görüntüler. Bu örnek, içinde en az bir TreeNodeTreeNodeCollectiontane olan bir TreeView ve üzerinde bir Label denetiminiz Formolmasını gerektirir.

void CopyTreeNodes()
{
   // Get the collection of TreeNodes.
   TreeNodeCollection^ myNodeCollection = myTreeView->Nodes;
   int myCount = myNodeCollection->Count;
   myLabel->Text = String::Concat( myLabel->Text, "Number of nodes in the collection : ", myCount );
   myLabel->Text = String::Concat( myLabel->Text, "\n\nElements of the Array after Copying from the collection :\n" );
   
   // Create an Object array.
   array<Object^>^myArray = gcnew array<Object^>(myCount);
   
   // Copy the collection into an array.
   myNodeCollection->CopyTo( myArray, 0 );
   for ( int i = 0; i < myArray->Length; i++ )
   {
      myLabel->Text = myLabel->Text + (dynamic_cast<TreeNode^>(myArray[ i ]))->Text + "\n";

   }
}
private void CopyTreeNodes()
{
   // Get the collection of TreeNodes.
   TreeNodeCollection myNodeCollection = myTreeView.Nodes;
   int myCount = myNodeCollection.Count;

   myLabel.Text += "Number of nodes in the collection :" + myCount;
   myLabel.Text += "\n\nElements of the Array after Copying from the collection :\n";
   // Create an Object array.
   Object[] myArray = new Object[myCount];
   // Copy the collection into an array.
   myNodeCollection.CopyTo(myArray,0);
   for(int i=0; i<myArray.Length; i++)
   {
      myLabel.Text += ((TreeNode)myArray[i]).Text + "\n";
   }
}
Private Sub CopyTreeNodes()
   ' Get the collection of TreeNodes.
   Dim myNodeCollection As TreeNodeCollection = myTreeView.Nodes
   Dim myCount As Integer = myNodeCollection.Count

   myLabel.Text += "Number of nodes in the collection :" + myCount.ToString()

   myLabel.Text += ControlChars.NewLine + ControlChars.NewLine + _
     "Elements of the Array after Copying from the collection :" + ControlChars.NewLine

   ' Create an Object array.
   Dim myArray(myCount -1) As Object

   ' Copy the collection into an array.
   myNodeCollection.CopyTo(myArray, 0)
   Dim i As Integer
   For i = 0 To myArray.Length - 1
      myLabel.Text += CType(myArray(i), TreeNode).Text + ControlChars.NewLine
   Next i
End Sub

Açıklamalar

Count özelliği, koleksiyona atanan nesne sayısını TreeNode tutar. Bir koleksiyonu yinelemek Count için döngünün üst sınırları olarak özellik değerini kullanabilirsiniz.

Not

Bir koleksiyonun dizin değeri sıfır tabanlı bir dizin olduğundan, döngü değişkeninden bir dizin çıkarmanız gerekir. Bunu hesaba eklemezseniz, koleksiyonun üst sınırlarını aşacak ve bir IndexOutOfRangeException özel durum oluşturacaksınız.

Şunlara uygulanır