惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

有赞技术团队
有赞技术团队
美团技术团队
博客园 - 司徒正美
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
阮一峰的网络日志
阮一峰的网络日志
S
SegmentFault 最新的问题
博客园_首页
雷峰网
雷峰网
V
V2EX
The Cloudflare Blog
博客园 - 三生石上(FineUI控件)
量子位
Last Week in AI
Last Week in AI
人人都是产品经理
人人都是产品经理
爱范儿
爱范儿
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
博客园 - 聂微东
V
Visual Studio Blog
Hugging Face - Blog
Hugging Face - Blog
博客园 - 【当耐特】
Jina AI
Jina AI
月光博客
月光博客
L
LangChain Blog

博客园 - 西门潇洒

MSSQL批量写入数据方案 关于SQL连接 加条件查询的LINQ表达式写法 关于LINQ中实现随机查询数据库中记录 .NET新特性--扩展方法 Array中数据强制数据类型转换 the Project type is not supported by installation 项目类型不能正确加载 ASP.NET三层架构中数据绑定的问题 ASP.NET 2.0中XML数据的处理 小牛生产小牛的问题解决集粹 <转>自毁前程的七种行为 <转>在 ASP.NET 中执行 URL 重写 正则式中的实用命名组替换 去除socket编程当中接收到的多余符\0 - 西门潇洒 - 博客园 将一段符合XML格式规范字符串插入已有XML文档当中 javascript正则表达式中使用变量关键字 <转>JavaScript 参考教程之对象化编程 <转>JavaScript 参考教程之JavaScript 简介 <转>JavaScript 参考教程之事件处理 <转>JavaScript 参考教程资料之文档对象
<转>如何C#中实现在TreeView查找某一节点
西门潇洒 · 2007-12-26 · via 博客园 - 西门潇洒

TreeView查找某一节点,通常有两种方法,一种是递归的,一种不是递归,但都是深度优先算法。其中,非递归方法效率高些,而递归算法要简洁一些。

第一种,递归算法,代码如下:

    private TreeNode FindNode( TreeNode tnParent, string strValue )

    {

        if( tnParent == null ) return null;

        if( tnParent.Text == strValue ) return tnParent;

        TreeNode tnRet = null;

        foreach( TreeNode tn in tnParent.Nodes )

        {

            tnRet = FindNode( tn, strValue );

            if( tnRet != null ) break;

        }

        return tnRet;

    }

第二种,非递归算法,代码如下:

    private TreeNode FindNode( TreeNode  tnParent, string strValue )

    {

        if( tnParent == null ) return null;

        if( tnParent.Text == strValue ) return tnParent;

        else if( tnParent.Nodes.Count == 0 ) return null;

        TreeNode tnCurrent, tnCurrentPar;

        //Init node

        tnCurrentPar = tnParent;

        tnCurrent = tnCurrentPar.FirstNode;

        while( tnCurrent != null && tnCurrent != tnParent )

        {

            while( tnCurrent != null )

            {

                if( tnCurrent.Text == strValue ) return tnCurrent;

                else if( tnCurrent.Nodes.Count > 0 )

                {

                    //Go into the deepest node in current sub-path

                    tnCurrentPar = tnCurrent;

                    tnCurrent = tnCurrent.FirstNode;

                }

                else if( tnCurrent != tnCurrentPar.LastNode )

                {

                    //Goto next sible node

                    tnCurrent = tnCurrent.NextNode;

                }

                else

                    break;

            }

            //Go back to parent node till its has next sible node

            while( tnCurrent != tnParent && tnCurrent == tnCurrentPar.LastNode )

            {

                tnCurrent = tnCurrentPar;

                tnCurrentPar = tnCurrentPar.Parent;

            }

            //Goto next sible node

            if( tnCurrent != tnParent )

                tnCurrent = tnCurrent.NextNode;

        }

        return null;

    }

       程序调用,如下:

        TreeNode tnRet = null;

        foreach( TreeNode tn in yourTreeView.Nodes )

        {

            tnRet =  FindNode( tn, yourValue );

            if( tnRet != null ) break;

        }

源自:

http://blog.csdn.net/Knight94/archive/2006/03/29/642736.aspx