从带有值的路径列表中填充 TreeView [英] Populate TreeView from list of paths with values

查看:20
本文介绍了从带有值的路径列表中填充 TreeView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个文本文件中有一些数据,格式如下:

I have some data in a text file which is formatted like this:

A.B.C=12
A.B.D=13
A.C.D=14

并且需要把它放到一个treeview控件中,使它看起来像这样:

and need to put it into a treeview control so that it looks like this:

两端的标签值应该等于它们在文本中所做的,即.C = 12.

The tag values of the ends should equal what they did in the text, ie. C = 12.

我尝试过的大部分内容都集中在在每一行上使用 foreach 循环,然后在 '.'s 和 '=' 上拆分字符串并循环遍历它们,但我一直无法得到它完全可以工作.

Most of what I've tried has been centered around using a foreach loop on each line, and then splitting the string on the '.'s and '=' and looping through those, but I have not been able to get it to work at all.

任何帮助将不胜感激...

Any help would be greatly appreciated...

推荐答案

这是解决您问题的一种方法.注释在代码中.适用于您的示例数据,但我不能保证它适用于其他一些情况:)

Here's one approach to your question. Comments are in code. Works with your sample data but I cannot guarantee it will work in some other cases :)

Main 方法是 PopulateTreeView() 所以从例如表单的 Load 事件调用它.此外,还有辅助方法 FindNode 用于搜索第一级节点以查找是否存在提供文本的节点.

Main method is PopulateTreeView() so call it from, in example, form's Load event. Also, there is helper method FindNode which is used to search through first level of nodes to find if node with supplied text exists.

如果您有其他问题,请随时提出.

If you have additional questions, feel free to ask.

private void PopulateTreeView()
{
    //read from file
    var lines = File.ReadAllLines(@"c:\temp\tree.txt");
    //go through all the lines
    foreach (string line in lines)
    {
        //split by dot to get nodes names
        var nodeNames = line.Split('.');
        //TreeNode to remember node level
        TreeNode lastNode = null;

        //iterate through all node names
        foreach (string nodeName in nodeNames)
        {
            //values for name and tag (tag is empty string by default)
            string name = nodeName;
            string tagValue = string.Empty;
            //if node is in format "name=value", change default values of name and tag value. If not, 
            if (nodeName.Contains("="))
            {
                name = nodeName.Split('=')[0];
                tagValue = nodeName.Split('=')[1];
            }

            //var used for finding existing node
            TreeNode existingNode = null;
            //new node to add to tree
            TreeNode newNode = new TreeNode(name);
            newNode.Tag = tagValue;
            //collection of subnodes to search for node name (to check if node exists)
            //in first pass, that collection is collection of treeView's nodes (first level)
            TreeNodeCollection nodesCollection = treeView1.Nodes;

            //with first pass, this will be null, but in every other, this will hold last added node.
            if (lastNode != null)
            {
                nodesCollection = lastNode.Nodes;
            }

            //look into collection if node is already there (method checks only first level of node collection)
            existingNode = FindNode(nodesCollection, name);
            //node is found? In that case, skip it but mark it as last "added"
            if (existingNode != null)
            {
                lastNode = existingNode;
                continue;
            }
            else //not found so add it to collection and mark node as last added.
            {
                nodesCollection.Add(newNode);
                lastNode = newNode;
            }
        }
    }

    treeView1.ExpandAll();
}

private TreeNode FindNode(TreeNodeCollection nodeCollectionToSearch, string nodeText)
{
    var nodesToSearch = nodeCollectionToSearch.Cast<TreeNode>();
    var foundNode = nodesToSearch.FirstOrDefault(n => n.Text == nodeText);
    return foundNode;
}

这篇关于从带有值的路径列表中填充 TreeView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆