簡體   English   中英

從路徑列表中填充值的TreeView

[英]Populate TreeView from list of paths with values

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

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

並需要將其放入Treeview控件中,使其看起來像這樣:

在此處輸入圖片說明

末端的標簽值應等於它們在文本中所做的值,即。 C = 12。

我嘗試過的大部分內容都集中在每行上使用一個foreach循環,然后在'。's和'='上拆分字符串,然后遍歷這些字符串,但我無法使其正常工作完全沒有

任何幫助將不勝感激...

這是解決您問題的一種方法。 注釋在代碼中。 使用您的示例數據,但我不能保證在其他情況下也可以使用它:)

主要方法是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;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM