2012-05-23 120 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