关闭

TreeView根据路径得到选择节点

标签: stringpath
688人阅读 评论(0) 收藏 举报
分类:
/// 根据路径得到选择节点
/// </summary>
/// <param name="path">路径</param>
/// <param name="treeView">树</param>
/// <returns>节点</returns>
private TreeNode GetNodeByPath(string path, TreeView treeView)
{
    TreeNode node = new TreeNode();
    string[] pathLevel = path.Split('//');

    int i = 0;
    foreach (TreeNode topNode in treeView.Nodes)
    {
        if (topNode.Text == pathLevel[i].ToString())
        {
            node = topNode;
            i++;
            break;
        }
    }

    if (i < pathLevel.Length)
    {
        node = GetSubNode(node, pathLevel, i);
    }


    return node;
}

private TreeNode GetSubNode(TreeNode node, string[] pathLevel, int i)
{
    TreeNode newNode = new TreeNode();
    foreach (TreeNode subNode in node.Nodes)
    {
        if (subNode.Text == pathLevel[i].ToString())
        {
            newNode = subNode;
            i++;
            if (i == pathLevel.Length)
            {
                break;
            }

            if (i < pathLevel.Length)
            {
                newNode = GetSubNode(newNode, pathLevel, i);
            }
        }
    }
    return newNode;

}
 
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:219845次
    • 积分:2944
    • 等级:
    • 排名:第12373名
    • 原创:15篇
    • 转载:274篇
    • 译文:0篇
    • 评论:20条
    最新评论