2012-05-23 225 views
1

我做了一些研究,我发现一些话题接近我的问题,但他们都没有解决它。从C列表填充treeview#

populate treeview from a list of path

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

SharpSvn: Getting repository structure and individual files

我想打一个库浏览器为我的SVN文件夹,以便用户可以选择一个文件夹,它会回归到一个文本框。

这是我的实际代码:

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

而且

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

但是我的树看起来就像这样:

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

但名称5名6应该是在名称4

对不起为长的职位,并感谢!

+0

在您的第一个'foreach(内容中的SvnListEventArgs项)'循环中,将第二个'if'更改为嵌套在第一个'if'中。如果路径为空白,您不希望第二个“if”运行。因为那么你最终只会再次为源主干调用'GetList'(IE:'“https:// sourcecode/svn/XXXXXX”+ path'将变成'https:// sourcecode/svn/XXXXXX' if if'path'是一个空字符串)。 – SPFiredrake

+0

没错,它不能解决我的问题,但我会改变它。 – LolCat

回答

3

if(item.Level == roots.Count)是你的问题我在想...你确定这些项目有正确的水平?例如,如果Name1Name4是根,那么在遇到第二个根后会发生什么?这是否给出了预期的结果:

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

只是一个想法。

+0

是的,这解决了它!谢谢! – LolCat