Treeview常用来导航,有时候需要有一个横向的路径式的导航,我们可以直接从treeview动态生成。这个内容让我对递归有了一些亲近的味道,以前总是怕怕。

 1         protected void TreeView1_SelectedNodeChanged(object sender, EventArgs e)
 2         {
 3 
 4             TreeNode theNode = TreeView1.SelectedNode;
 5 
 6             Label1.Text = getTextPath(theNode);
 7         }
 8 
 9         //生成从当前节点到根节点的路径
10         public string getTextPath(TreeNode theNode)
11         {
12             string result = theNode.Text;
13 
14             if (theNode.Parent != null)
15             {
16                 result = theNode.Parent.Text + " >> " + result;
17 
18                 getTextPath(theNode.Parent);
19             }
20 
21             return result;
22         }

 

一个多简单的递归!结果如下图:

Treeview中,递归生成从当前选中节点到根节点的全路径

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-12-08
  • 2022-12-23
  • 2022-12-23
  • 2021-12-19
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-12-05
  • 2021-10-28
  • 2021-10-08
  • 2022-12-23
  • 2021-11-02
相关资源
相似解决方案