TreeNode.GetNodeCount(Boolean) 方法

定义

返回子树节点的数目。

public int GetNodeCount (bool includeSubTrees);

参数

includeSubTrees
Boolean

如果所得计数包括不直接以此树节点为根的所有树节点,则为 true;否则为 false

返回

分配给 Nodes 集合的子树节点的数目。

示例

下面的代码示例设置 PathSeparatorTreeView 属性,并显示 包含在 TreeNodeCollection 中的 SelectedNode子树节点数。 还会显示树视图控件中子树节点占树节点总数的百分比。 此示例要求具有 FormButton具有 的 , TreeView 而控件包含 TreeNodeCollection 多个 TreeNode 对象 (最好具有三个或更多级别的) 。

private void myButton_Click(object sender, System.EventArgs e)
{
   // Set the tree 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.");
}

适用于

产品 版本
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9

另请参阅