TreeNodeCollection.Count Vlastnost

Definice

Získá celkový počet TreeNode objektů v kolekci.

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

Hodnota vlastnosti

Int32

Celkový počet TreeNode objektů v kolekci

Implementuje

Atributy

Příklady

Následující příklad kódu zobrazí počet TreeNode objektů v objektu TreeNodeCollection, zkopíruje obsah kolekce do Object pole a zobrazí seznam uzlů stromu v ovládacím Label prvku. Tento příklad vyžaduje, abyste měli TreeView alespoň jeden v jeho TreeNodeCollectionaspoň jednom TreeNode ovládacím Label Formprvku .

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

Poznámky

Vlastnost Count obsahuje počet TreeNode objektů přiřazených kolekci. Hodnotu vlastnosti můžete použít Count jako horní hranice smyčky k iteraci kolekcí.

Poznámka

Vzhledem k tomu, že hodnota indexu kolekce je index založený na nule, musíte odečíst jednu z proměnné smyčky. Pokud tento účet nezapočítáte, překročíte horní hranice kolekce a vyvoláte IndexOutOfRangeException výjimku.

Platí pro