2010-12-13 17 views
1

我在線找到了這段代碼。它可以工作,但由於某種原因它會加載文件目錄兩次。什麼可能導致它多次填充此樹視圖?

namespace DockSample.Controls 
{ 
    using System; 
    using System.Collections.Generic; 
    using System.Text; 
    using System.Windows.Forms; 
    using System.IO; 
    using System.Drawing; 
    using Microsoft.VisualBasic.FileIO; 
    using DockSample; 



    //TODO: Add options for filtering by robot 
     public class SolutionExplorer : TreeView 
     { 

      public SolutionExplorer() 
      { 
       this.BeforeExpand += customBeforeExpand; 
       this.Nodes.Clear(); 
       CreateTree(this); 
      } 

      private bool CreateTree(TreeView treeView) 
      { 

       bool returnValue = false; 

       try 
       { 
        // Create Desktop 
        TreeNode desktop = new TreeNode(); 
        // desktop.Text = "Desktop"; 
        // desktop.Tag = "Desktop"; 
        // desktop.Nodes.Add(""); 
        // treeView.Nodes.Add(desktop); 
        // Get driveInfo 
        foreach (DriveInfo drv in DriveInfo.GetDrives()) 
        { 

         TreeNode fChild = new TreeNode(); 
         if (drv.DriveType == DriveType.CDRom) 
         { 
          fChild.ImageIndex = 1; 
          fChild.SelectedImageIndex = 1; 
         } 
         else if (drv.DriveType == DriveType.Fixed) 
         { 
          fChild.ImageIndex = 0; 
          fChild.SelectedImageIndex = 0; 
         } 
         fChild.Text = drv.Name; 
         fChild.Nodes.Add(""); 
         treeView.Nodes.Add(fChild); 
         returnValue = true; 
        } 

       } 
       catch 
       { 
        returnValue = false; 
       } 
       return returnValue; 

      } 

      /* Method :EnumerateDirectory 
      * Author : Chandana Subasinghe 
      * Date : 10/03/2006 
      * Discription : This is use to Enumerate directories and files 
      * 
      */ 
      public TreeNode EnumerateDirectory(TreeNode parentNode, List<string> thisFilter) 
      { 

       try 
       { 
        DirectoryInfo rootDir; 

        // To fill Desktop 
        Char[] arr = { '\\' }; 
        string[] nameList = parentNode.FullPath.Split(arr); 
        string path = ""; 

        if (nameList.GetValue(0).ToString() == "Desktop") 
        { 
         path = SpecialDirectories.Desktop + "\\"; 

         for (int i = 1; i < nameList.Length; i++) 
         { 
          path = path + nameList[i] + "\\"; 
         } 

         rootDir = new DirectoryInfo(path); 
        } 
        // for other Directories 
        else 
        { 

         rootDir = new DirectoryInfo(parentNode.FullPath + "\\"); 
        } 

        parentNode.Nodes[0].Remove(); 
        foreach (DirectoryInfo dir in rootDir.GetDirectories()) 
        { 

         TreeNode node = new TreeNode(); 
         node.Text = dir.Name; 
         node.Nodes.Add(""); 
         parentNode.Nodes.Add(node); 
        } 
        //Fill files 
        foreach (FileInfo file in rootDir.GetFiles()) 
        { 
         if (isValidFilter(getExtention(file.Name))) 
         { 
          TreeNode node = new TreeNode(); 
          node.Text = file.Name; 
          node.ImageIndex = 2; 
          node.SelectedImageIndex = 2; 
          parentNode.Nodes.Add(node); 
         } 
        } 



       } 

       catch 
       { 
       } 

       return parentNode; 
      } 
      private bool isValidFilter(string ext) 
      { 
       bool result = false; 
       foreach(string s in filter) 
       { 
        if (ext == s) 
         result = true; 

       } 

       return result; 
      } 
      private string getExtention(string filename) 
      { 
       return filename.Substring(filename.IndexOf(".")); 
      } 
      private void customBeforeExpand(object sender, TreeViewCancelEventArgs e) 
      { 
       if (e.Node.Nodes[0].Text == "") 
       { 
        TreeNode node = this.EnumerateDirectory(e.Node,filter); 
       } 
      } 
      private List<string> filter = new List<string>(); 
      public List<string> Filter 
      { 
       get { return filter; } 
       set { filter = value; } 
      } 
      private void InitializeComponent() 
      { 
       this.SuspendLayout(); 
       this.ResumeLayout(false); 

      } 
     } 

} 
+0

這就是當您從互聯網獲取代碼時發生的情況。我可能會建議縮小范例。 – tster 2010-12-13 20:06:35

+1

你可能也想顯示調用代碼 – jcolebrand 2010-12-13 20:10:29

+0

樹是在構造函數上創建的,在這種情況下,我叫它,我執行this.Nodes.Clear(),我認爲這將刪除所有現有的節點。 – mookie 2010-12-13 20:12:42

回答

2

控件的構造函數在設計時和運行時均運行。因此,只要您將控件放在窗體上,它將填充樹視圖。問題是,節點將被序列化爲InitializeComponent()。看看你的表單的Designer.cs文件,你會發現它們在那裏。當你運行表單時,構造函數再次運行,加倍列表。

您需要防止構造函數在設計時添加節點。這有點難,通常使用DesignMode屬性,但它在構造函數中沒有設置爲true。像這樣做,而不是:

protected override void OnHandleCreated(EventArgs e) { 
    base.OnHandleCreated(e); 
    if (!DesignMode && treeView.Nodes.Count == 0) { 
     CreateTree(this); 
    } 
} 

或通過添加您在窗體的構造函數或方法的OnLoad調用公共方法做它明確。這是相當明智的,你可能想要捕捉異常。當你修改文件系統時總是可能的。