用FullPath属性来判断吧。 TreeView1.SelectedNode.FullPath: 路径从根树节点开始,包括要达到该树节点而必须浏览的所有树节点的标签。分隔节点标签的分隔符在包含该节点的 TreeView 控件的 PathSeparator 属性中指定。例如,如果名为“Location”的树视图控件的分隔符设置为反斜杠字符 (\),FullPath 属性值就为“Country\Region\State”。下面的代码示例设置 TreeView 的 PathSeparator 属性并显示 SelectedNode 的 TreeNodeCollection 中包含的子树节点数。还显示树视图控件中子树节点占全部树节点的百分比。本示例要求有一个包含 Button 的 Form 和一个包含 TreeNodeCollection 的 TreeView 控件,该集合包含若干个 TreeNode 对象(最好具有三级或三级以上)。 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."); }
TreeView1.SelectedNode.Level也行的。
if (this.treeView1.SelectedNode.Level == 4) { //节点深度为4时的操作 }
TreeView1.SelectedNode.FullPath:
路径从根树节点开始,包括要达到该树节点而必须浏览的所有树节点的标签。分隔节点标签的分隔符在包含该节点的 TreeView 控件的 PathSeparator 属性中指定。例如,如果名为“Location”的树视图控件的分隔符设置为反斜杠字符 (\),FullPath 属性值就为“Country\Region\State”。下面的代码示例设置 TreeView 的 PathSeparator 属性并显示 SelectedNode 的 TreeNodeCollection 中包含的子树节点数。还显示树视图控件中子树节点占全部树节点的百分比。本示例要求有一个包含 Button 的 Form 和一个包含 TreeNodeCollection 的 TreeView 控件,该集合包含若干个 TreeNode 对象(最好具有三级或三级以上)。
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.");
}
if (this.treeView1.SelectedNode.Level == 4)
{
//节点深度为4时的操作
}