This documentation is archived and is not being maintained.

TreeNode.GetNodeCount Method

Returns the number of child tree nodes.

[Visual Basic]
Public Function GetNodeCount( _
   ByVal includeSubTrees As Boolean _
) As Integer
[C#]
public int GetNodeCount(
 bool includeSubTrees
);
[C++]
public: int GetNodeCount(
 bool includeSubTrees
);
[JScript]
public function GetNodeCount(
   includeSubTrees : Boolean
) : int;

Parameters

includeSubTrees
true if the resulting count includes all tree nodes indirectly rooted at this tree node; otherwise, false.

Return Value

The number of child tree nodes assigned to the Nodes collection.

Example

[Visual Basic, C#, C++] The following example set the PathSeparator proprety of a TreeView and displays the number of child tree nodes 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 assumes 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).

[Visual Basic] 
Private Sub myButton_Click(ByVal sender As Object, _
  ByVal e As System.EventArgs) Handles myButton.Click
   ' Set the tre 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

[C#] 
private void myButton_Click(object sender, System.EventArgs e)
{
   // Set the tre view's PathSeparator property.
   myTreeView.PathSeparator = ".";

   // Get the count of the child tree nodes contained in the SelectedNode.
   int myNodeCount = myTreeView.SelectedNode.GetNodeCount(true);
   decimal myChildPercentage = ((decimal)myNodeCount/
     (decimal)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.\nThat is " 
     + string.Format("{0:###.##}", myChildPercentage) 
     + "% of the total tree nodes in the tree view control.");
}

[C++] 
private:
    void myButton_Click(Object* /*sender*/, System::EventArgs* /*e*/) {
        // Set the tre view's PathSeparator property.
        myTreeView->PathSeparator = S".";

        // Get the count of the child tree nodes contained in the SelectedNode.
        int myNodeCount = myTreeView->SelectedNode->GetNodeCount(true);
        Decimal myChildPercentage = ((Decimal)myNodeCount/
            (Decimal)myTreeView->GetNodeCount(true)) * 100;

        // Display the tree node path and the number of child nodes it and the tree view have.
        MessageBox::Show(String::Concat(S"The '", myTreeView->SelectedNode->FullPath,
            S"' node has ", __box(myNodeCount), S" child nodes.\nThat is ",
            String::Format(S"{0:###.##}", __box(myChildPercentage)),
            S"% of the total tree nodes in the tree view control."));
    }

[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

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

Show: