通过判断TreeNode对象的Parent属性是否为null
int intLayerCount = 1;
TreeNode treeNodeTemp = treeNodeSelected; //假定treeNodeSelected为当前选择的节点
while (treeNodeTemp.Parent != null)
{
intLayerCount++;
treeNodeTemp = treeNodeTemp.Parent;
}
private void button1_Click(object sender, EventArgs e)
{
int i= this.treeView1.Nodes.Count;
foreach (TreeNode t in this.treeView1.Nodes)
{
count = count + 1;
getNode(t,false);
}
this.button1.Text = count.ToString();
}
private void getNode(TreeNode t,bool b)
{
if (b)
{
count = count + 1;
}
for(int i=0;i<t.Nodes.Count;i++)
{
getNode(t.Nodes[i],true);
}
}
所有節點遞歸