Count Property

TreeNodeCollection.Count Property

Gets the total number of TreeNode objects in the collection.

[Visual Basic]
Public Overridable ReadOnly Property Count As Integer  Implements _
   ICollection.Count
[C#]
public virtual int Count {get;}
[C++]
public: __property virtual int get_Count();
[JScript]
public function get Count() : int;

Property Value

The total number of TreeNode objects in the collection.

Implements

ICollection.Count

Remarks

The Count property holds the number of TreeNode objects assigned to the collection. You can use the Count property value as the upper bounds of a loop to iterate through a collection.

Note   Because the index value of a collection is a zero-based index, you must subtract one from the looping variable. If you do not account for this, you will exceed the upper bounds of the collection and throw an IndexOutOfRangeException exception.

Example

[Visual Basic, C#, C++] The following example displays the number of TreeNode objects in a TreeNodeCollection and copies the contents of the collection to an Object array and displays a list of the tree nodes in a Label control. This example assumes you have a TreeView with at least one TreeNode in its TreeNodeCollection and a Label control on a Form.

[Visual Basic] 
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

[C#] 
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";
   }
}

[C++] 
private:
    void CopyTreeNodes() {
        // Get the collection of TreeNodes.
        TreeNodeCollection* myNodeCollection = myTreeView->Nodes;
        int myCount = myNodeCollection->Count;

        myLabel->Text = String::Concat(myLabel->Text, 
            S"Number of nodes in the collection : ", __box(myCount));
        myLabel->Text = String::Concat(myLabel->Text, 
            S"\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 = String::Concat(myLabel->Text, 
                (dynamic_cast<TreeNode*>(myArray->Item[i]))->Text, S"\n");
        }
    }

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework

See Also

TreeNodeCollection Class | TreeNodeCollection Members | System.Windows.Forms Namespace

Show:
© 2016 Microsoft