简体   繁体   English

从C#中的列表填充树视图

[英]Populate treeview from list in C#

I've made some researches and I found some topic close to my problem, but none of them solved it. 我做了一些研究,发现一些话题与我的问题很接近,但没有一个能解决它。

populate treeview from a list of path 从路径列表填充树视图

http://msdn.microsoft.com/en-us/library/system.windows.forms.treeview.aspx http://msdn.microsoft.com/en-us/library/system.windows.forms.treeview.aspx

http://social.msdn.microsoft.com/Forums/en/winforms/thread/dae1c72a-dd28-4232-9aa4-5b38705c0a97 http://social.msdn.microsoft.com/Forums/en/winforms/thread/dae1c72a-dd28-4232-9aa4-5b38705c0a97

SharpSvn: Getting repository structure and individual files SharpSvn:获取存储库结构和单个文件

I want to make a repository browser for my SVN folder so that the user can choose one folder and it will be return to a text box. 我想为我的SVN文件夹创建一个存储库浏览器,以便用户可以选择一个文件夹,它将返回到文本框。

This is my actual code : 这是我的实际代码:

    private void sourceTrunkBrowseButton_Click(object sender, EventArgs e)
     {
         using (SvnClient svnClient = new SvnClient())
         {
             Collection<SvnListEventArgs> contents;
             Collection<SvnListEventArgs> contents2;
             List<TreeItem> files = new List<TreeItem>();
             if (svnClient.GetList(new Uri("https://sourcecode/svn/XXXXXX"), out contents))
             {
                 foreach (SvnListEventArgs item in contents)
                 {
                     if (item.Path != "")
                     {
                         files.Add(new TreeItem(item.Path, 0));

                         if (svnClient.GetList(new Uri("https://sourcecode/svn/XXXXX" + item.Path), out contents2) && item.Path != "")
                         {
                             foreach (SvnListEventArgs item2 in contents2)
                             {
                                 if (item2.Path != "")
                                 {
                                     files.Add(new TreeItem(item2.Path, 1));
                                 }
                             }
                         }
                     }
                 }
             }
             svnBrowser_.FillMyTreeView(files);
             svnBrowser_.Show();
         }
     }

And

  public void FillMyTreeView(List<AutoTrunk.TreeItem> files) 
 {

       // Suppress repainting the TreeView until all the objects have been created.
        svnTreeView.BeginUpdate();

        svnTreeView.Nodes.Clear();
        List<TreeNode> roots = new List<TreeNode>();
         roots.Add(svnTreeView.Nodes.Add("Items"));
         foreach (AutoTrunk.TreeItem item in files)
     {
         if (item.Level == roots.Count) roots.Add(roots[roots.Count - 1].LastNode);
         roots[item.Level].Nodes.Add(item.BrowsePath);
     }

        // Begin repainting the TreeView.
        svnTreeView.EndUpdate();
 }

But my tree look like this : 但我的树看起来像这样:

+---Name1
|   |
|   +------Name2
|   |
|   +------Name3
|   |
|   +------Name5
|   |
|   +------Name6
|
+---Name4

but Name 5 and Name 6 should be under Name 4 但名称5和名称6应在名称4下

Sorry for the long post and thanks! 对不起,发帖很长,谢谢!

if(item.Level == roots.Count) is your problem I'm thinking... Are you sure the items have the correct level? if(item.Level == roots.Count)是你的问题我在想......你确定这些项目的级别是正确的吗? For example, if Name1 and Name4 are roots, then what happens after you've encountered your second root? 例如,如果Name1Name4是根,你所遇到的第二根后,会发生什么情况? Does this give the intended result: 这是否给出了预期的结果:

TreeNode root = svnTreeView.Nodes.Add("Items");
TreeNode workingNode = root;
foreach (AutoTrunk.TreeItem item in files)
{
    if (item.Level == 0) 
        workingNode = root.Nodes.Add(item.BrowsePath);
    else
        workingNode.Nodes.Add(item.BrowsePath);
}

Just a thought. 只是一个想法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM