TreeView 查找指定节点

/// <summary>
    /// 查找指定节点
    /// </summary>
    /// <param name="tnParent">节点</param>
    /// <param name="strValue">值</param>
    /// <returns></returns>
    private TreeNode FindNode(TreeNode tnParent, string strValue)
    {
        TreeNode treeNode2 = new TreeNode();
        string venueCode = "";
        if (tnParent == null) return null;
        if (tnParent.Value == strValue) return tnParent;
        tnParent.Expand();
        TreeNode tnRet = null;
        foreach (TreeNode tn in tnParent.ChildNodes)
        {
            //获得节点Value
            if (venueCode == "")
            {
                //记录节点
                treeNode2 = tn;
                venueCode = tn.Value;
            }
            else
            {
                //当选择节点发生变化时
                if (venueCode != tn.Value)
                {
                    treeNode2.Collapse();
                    venueCode = tn.Value;
                    treeNode2 = tn;
                }
            }

            tnRet = FindNode(tn, strValue);
            if (tnRet != null) break;
        }
        return tnRet;
    }

方法调用如下:
//循环查找节点


foreach (TreeNode node in this.TreeView1.Nodes)
{
    TreeNode treeNode = FindNode(node, venueCode);

    if (treeNode != null)
    {
        treeNode.CollapseAll();
        treeNode.Selected = true;
        state = 1;
        break;
     }
}

 

posted on 2014-08-16 18:07  Haustbule  阅读(1095)  评论(0编辑  收藏  举报