TreeView.GetNodeCount Method

Retrieves the number of tree nodes, optionally including those in all subtrees, assigned to the tree view control.

Namespace:  System.Windows.Forms
Assembly:  System.Windows.Forms (in System.Windows.Forms.dll)

Public Function GetNodeCount ( _
	includeSubTrees As Boolean _
) As Integer


Type: System.Boolean

true to count the TreeNode items that the subtrees contain; otherwise, false.

Return Value

Type: System.Int32
The number of tree nodes, optionally including those in all subtrees, assigned to the tree view control.

If includeSubTrees is true, the result is the number of all the tree nodes in the entire tree structure.

The following code example sets the PathSeparator property of a TreeView and displays the number of child tree nodes that are contained in the TreeNodeCollection of the SelectedNode. The percentage of child tree node to total tree nodes in the tree view control is also displayed. This example requires that you have a Form with a Button, and a TreeView control with a TreeNodeCollection that has several TreeNode objects (preferably with three or more levels).

Private Sub myButton_Click(ByVal sender As Object, _
  ByVal e As System.EventArgs) Handles myButton.Click
   ' Set the tree view's PathSeparator property.
   myTreeView.PathSeparator = "." 

   ' Get the count of the child tree nodes contained in the SelectedNode. 
   Dim myNodeCount As Integer = myTreeView.SelectedNode.GetNodeCount(True)
   Dim myChildPercentage As Decimal = CDec(myNodeCount) / _
      CDec(myTreeView.GetNodeCount(True)) * 100

   ' Display the tree node path and the number of child nodes it and the tree view have.
   MessageBox.Show(("The '" + myTreeView.SelectedNode.FullPath + "' node has " _
      + myNodeCount.ToString() + " child nodes." + Microsoft.VisualBasic.ControlChars.Lf _
      + "That is " + String.Format("{0:###.##}", myChildPercentage) _
      + "% of the total tree nodes in the tree view control."))
End Sub

.NET Framework

Supported in: 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
© 2014 Microsoft