Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/extjs/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用值从路径列表填充TreeView_C#_Winforms_Treeview - Fatal编程技术网

C# 使用值从路径列表填充TreeView

C# 使用值从路径列表填充TreeView,c#,winforms,treeview,C#,Winforms,Treeview,我在文本文件中有一些数据,其格式如下: A.B.C=12 A.B.D=13 A.C.D=14 需要将其放入treeview控件中,使其看起来像这样: 末端的标记值应等于它们在文本中所做的值,即C=12 我所做的大部分工作都是围绕着在每一行上使用foreach循环,然后在“.”和“=”上拆分字符串并在这些行中循环,但我根本无法让它工作 任何帮助都将不胜感激……这里有一种解决您问题的方法。注释用代码表示。 适用于您的示例数据,但我不能保证它在其他一些情况下也适用:) Main方法是Populat

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

A.B.C=12
A.B.D=13
A.C.D=14
需要将其放入treeview控件中,使其看起来像这样:

末端的标记值应等于它们在文本中所做的值,即C=12

我所做的大部分工作都是围绕着在每一行上使用foreach循环,然后在“.”和“=”上拆分字符串并在这些行中循环,但我根本无法让它工作


任何帮助都将不胜感激……

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

Main方法是
PopulateTreeView()
,因此可以从表单的
Load
事件调用它。此外,还有一个helper方法
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;
}
private void PopulateTreeView()
{
//从文件中读取
var lines=File.ReadAllLines(@“c:\temp\tree.txt”);
//通关
foreach(行中的字符串行)
{
//按点拆分以获取节点名称
var nodeNames=line.Split('.');
//要记住节点级别的树节点
TreeNode lastNode=null;
//遍历所有节点名称
foreach(nodeName中的字符串nodeName)
{
//名称和标记的值(默认情况下,标记为空字符串)
字符串名=节点名;
string tagValue=string.Empty;
//如果节点的格式为“名称=值”,则更改名称和标记值的默认值。如果不是,
if(nodeName.Contains(“=”)
{
name=nodeName.Split('=')[0];
tagValue=nodeName.Split('=')[1];
}
//用于查找现有节点的var
TreeNode existingNode=null;
//要添加到树中的新节点
TreeNode newNode=新的TreeNode(名称);
Tag=tagValue;
//用于搜索节点名称的子节点集合(用于检查节点是否存在)
//在第一个过程中,该集合是treeView节点的集合(第一级)
TreeNodeCollection节点Collection=树视图1.节点;
//对于第一次传递,它将为null,但每隔一次,它将保存最后添加的节点。
if(lastNode!=null)
{
NodeCollection=lastNode.Nodes;
}
//若节点已经存在,则查看集合(方法仅检查节点集合的第一级)
existingNode=FindNode(NodeCollection,名称);
//找到节点?在这种情况下,跳过它,但将其标记为最后一个“已添加”
if(existingNode!=null)
{
lastNode=现有节点;
继续;
}
else//未找到,因此将其添加到集合中,并将节点标记为上次添加的节点。
{
nodesCollection.Add(newNode);
lastNode=newNode;
}
}
}
treeView1.ExpandAll();
}
私有TreeNode FindNode(TreeNodeCollection nodeCollectionToSearch,string nodeText)
{
var nodesToSearch=nodeCollectionToSearch.Cast();
var foundNode=nodesToSearch.FirstOrDefault(n=>n.Text==nodeText);
返回foundNode;
}
字符串拆分('.')是一个良好的开端。张贴你的代码,我们将从那里开始。