博客园  :: 首页  :: 新随笔  :: 联系 :: 管理

TreeView检索节点所含关键字

Posted on 2011-04-13 10:51  codingsilence  阅读(208)  评论(0编辑  收藏  举报

  /// <summary>
    /// TReeView操作的类
    /// </summary>
    public class TreeViewOpt
    {
       /// <summary>
       /// 根据节点名称遍历树节点
       /// </summary>
       /// <param name="NodeText">节点名称包含的关键字</param>
       /// <param name="tnParent">节点集合</param>
       /// <returns>查找到的第一个节点</returns>
        public static TreeNode FindTreeNode(string NodeText, TreeNode tnParent)
        {
            if (tnParent == null)
                return null;
            if (tnParent.Text.IndexOf(NodeText)>=0)
                return tnParent;
            TreeNode tnRet = null;
            foreach (TreeNode tn in tnParent.Nodes)
            {
                tnRet = FindTreeNode(NodeText, tn);
                if (tnRet != null)
                    break;
            }
            return tnRet;
        }
        /// <summary>
        /// 根据节点名称遍历树节点
        /// </summary>
        /// <param name="NodeText">节点名称包含的关键字</param>
        /// <param name="treeView">TreeView对象</param>
        /// <returns>查找到的第一个节点</returns>
        public static TreeNode CallFindNode(string NodeText, TreeView treeView)
        {
            TreeNodeCollection nodes = treeView.Nodes;
            foreach (TreeNode n in nodes)
            {
                TreeNode temp = FindTreeNode(NodeText, n);
                if (temp != null)
                    return temp;
            }
            return null;
        }
    }