将文件修订填充到Treeview [英] Populate file revision to treeview

查看:77
本文介绍了将文件修订填充到Treeview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试将文件修订列表填充到treeview.我有一个修订文件列表,如下所示.

I try to populate list of file revision to treeview. I have a list of revision file like a below.

1.17
1.17.1.1
1.17.1.2
1.17.1.2.1.1    
1.17.1.2.1.2
1.17.1.2.1.2.1.1    
1.17.1.2.1.2.1.2
1.17.1.2.1.2.1.3
1.17.1.2.1.2.1.3.1.1
1.17.1.2.1.2.1.3.1.2
1.17.1.2.1.2.1.3.2.1
1.17.1.2.1.2.1.4    
1.17.1.2.1.2.1.5
1.17.1.2.1.2.1.5.1.1
1.17.1.2.1.2.1.5.1.2
1.17.1.2.1.2.1.5.1.2.1.1
1.17.1.2.1.3
1.17.1.2.1.4
1.18
1.19

现在,我想像这样填充它

Now, I would like to populate it like a

1.17
    1.17.1.1
    1.17.1.2
        1.17.1.2.1.1    
        1.17.1.2.1.2
            1.17.1.2.1.2.1.1    
            1.17.1.2.1.2.1.2
            1.17.1.2.1.2.1.3
                1.17.1.2.1.2.1.3.1.1
                1.17.1.2.1.2.1.3.1.2
                1.17.1.2.1.2.1.3.2.1    
            1.17.1.2.1.2.1.4    
            1.17.1.2.1.2.1.5    
                1.17.1.2.1.2.1.5.1.1
                1.17.1.2.1.2.1.5.1.2    
                    1.17.1.2.1.2.1.5.1.2.1.1        
        1.17.1.2.1.3
        1.17.1.2.1.4                        
1.18
1.19

我的想法按级别分类

1.17         -> level 1
1.17.1.1     -> level 2
1.17.1.2     -> level 2
1.17.1.2.1.1 -> level 3
...

然后我使用开关盒将其填充到树状视图

Then I use switch case to populate it to treeview

if level = 1 then parent node
else if level = 2 then child node
else if level = 3 then grandchild node
...

但是,我不知道这是实现我的目标的正确方法.有没有更好的方法可以做到这一点?

However, I don't know is it a right way to achieve my purpose. Is there any better way to do that?

感谢任何帮助,并非常感谢

Any help are appreciate and thanks so much

推荐答案

您可以利用TreeNodes路径(#.#)的重复模式.使用RegEx创建一个数组.如果数组中只有一项,则该路径是根节点的路径,否则以字符串形式连接除最后一项以外的数组(以连接父项的名称),并使用 TreeView.Nodes.Find(..)函数以查找每个路径的父节点.

You can take advantage of the repeated pattern of the TreeNodes paths ( #.# ). Create an array of them using the RegEx. If there is only one item in the array, then the path is of the root node, otherwise string join the array except the last item (to concat the parent's name) and use the TreeView.Nodes.Find(..) function to find the parent node of each path.

C#

private void PopulateTree(IEnumerable<string> paths)
{
    treeView1.BeginUpdate();
    treeView1.Nodes.Clear();

    foreach (var path in paths.OrderBy(x => x))
    {
        var node = new TreeNode { Name = path, Text = path };
        var arr = Regex.Matches(path, @"\d+.\d+")
            .Cast<Match>().Select(x => x.Value).ToArray();

        if (arr.Count() == 1)
            treeView1.Nodes.Add(node);
        else
        {
            var parentPath = string.Join(".", arr, 0, arr.Count() - 1);
            var parentNode = treeView1.Nodes.Find(parentPath, true).FirstOrDefault();

            if (parentNode != null)
                parentNode.Nodes.Add(node);
        }
    }

    treeView1.EndUpdate();
}

private void TheCaller()
{
    var revisionsList = //Get the list...

    PopulateTree(revisionsList);    
}

VB.NET

Private Sub PopulateTree(paths As IEnumerable(Of String))
    TreeView1.BeginUpdate()
    TreeView1.Nodes.Clear()

    For Each path In paths.OrderBy(Function(x) x)
        Dim node As New TreeNode With {.Name = path, .Text = path}
        Dim arr = Regex.Matches(path, "\d+.\d+").
            Cast(Of Match).Select(Function(x) x.Value).ToArray()

        If arr.Count = 1 Then
            TreeView1.Nodes.Add(node)
        Else
            Dim parentPath = String.Join(".", arr, 0, arr.Count() - 1)
            Dim parentNode = TreeView1.Nodes.Find(parentPath, True).FirstOrDefault

            If parentNode IsNot Nothing Then
                parentNode.Nodes.Add(node)
            End If
        End If
    Next

    TreeView1.EndUpdate()
End Sub

Private Sub TheCaller()
    Dim revisionsList = 'Get the list...

    PopulateTree(revisionsList)
End Sub

结果是:

这篇关于将文件修订填充到Treeview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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