【问题标题】:Populate TreeView from list of paths with values从具有值的路径列表中填充 TreeView
【发布时间】:2017-04-10 18:56:59
【问题描述】:

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

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

并需要将其放入树视图控件中,使其看起来像这样:

结尾的标记值应该等于它们在文本中所做的,即。 C = 12。

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

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

【问题讨论】:

  • 字符串拆分 ('.') 是一个好的开始。发布您拥有的代码,我们将从那里开始。

标签: c# winforms treeview


【解决方案1】:

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

主要方法是PopulateTreeView(),因此可以从表单的Load 事件中调用它。此外,还有一个辅助方法FindNode,它用于搜索第一级节点以查找提供文本的节点是否存在。

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

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;
}

【讨论】:

    猜你喜欢
    • 2010-11-12
    • 2015-05-13
    • 1970-01-01
    • 2016-06-27
    • 1970-01-01
    • 2013-07-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-18
    相关资源
    最近更新 更多