• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
somesongs - day day up
别着急,慢慢来吧。
博客园    首页    新随笔    联系   管理    订阅  订阅

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

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         }

 

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


posted @ 2009-02-02 21:43  somesongs  阅读(972)  评论(3)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3