Advertisement
Guest User

TreeView update

a guest
Sep 11th, 2013
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.80 KB | None | 0 0
  1. namespace TestTreeViewThread
  2. {
  3.     public partial class Form1 : Form
  4.     {
  5.         public Form1()
  6.         {
  7.             InitializeComponent();
  8.         }
  9.  
  10.         /// <summary>
  11.         /// Holds the root node of the TreeView added last
  12.         /// </summary>
  13.        // TreeNode tn;
  14.  
  15.         /// <summary>
  16.         /// Fills up the TreeView.
  17.         /// </summary>
  18.         //public void FillTree()
  19.         //{
  20.         //    // Create 10 root nodes
  21.         //    for (int i = 0; i < 10; i++)
  22.         //    {
  23.         //        //Create the node, here we must call on the form's thread otherwise we get an exception:
  24.         //        //Action being performed on this control is being called from the wrong thread. Marshal
  25.         //        //to the correct thread using Control.Invoke or Control.BeginInvoke to perform this action.
  26.         //        treeView1.Invoke(new Add(Add1), new object[] { i });
  27.         //        //Create 10 subnodes for each root node
  28.         //        for (int j = 0; j < 10; j++)
  29.         //        {
  30.         //            //Create the node, here we must call on the form's thread otherwise we get an exception:
  31.         //            //Action being performed on this control is being called from the wrong thread. Marshal
  32.         //            //to the correct thread using Control.Invoke or Control.BeginInvoke to perform this action.
  33.         //            treeView1.Invoke(new Add(Add2), new object[] { i * 10 + j });
  34.         //            //Slow down the filling process
  35.         //            //System.Threading.Thread.Sleep(1000);
  36.         //        }
  37.         //        //toolStripProgressBar1.PerformStep();
  38.         //    }
  39.         //    //treeView1.Update();
  40.         //}
  41.  
  42.         public void ListDirectory(DirectoryInfo path)
  43.         {
  44.             treeView1.Nodes.Add(CreateDirectoryNode(path));
  45.         }
  46.  
  47.        
  48.         public void Addnode(DirectoryInfo dirinfo)
  49.         {
  50.             Invoke(new AddCDAnode(ListDirectory), new object[] { dirinfo });
  51.         }
  52.  
  53.  
  54.         private TreeNode CreateDirectoryNode(DirectoryInfo directoryInfo)
  55.         {
  56.             var directoryNode = new TreeNode(directoryInfo.Name);
  57.             foreach (var directory in directoryInfo.GetDirectories())
  58.             {
  59.                 //Statustext = directory.FullName;
  60.                 directoryNode.Nodes.Add(CreateDirectoryNode(directory));
  61.             }
  62.             foreach (var file in directoryInfo.GetFiles())
  63.                 directoryNode.Nodes.Add(new TreeNode(file.Name));
  64.             return directoryNode;
  65.         }
  66.  
  67.        
  68.         public delegate void AddCDAnode(DirectoryInfo dirinfo);
  69.        
  70.         //public delegate void Add(int i);
  71.  
  72.         /// <summary>
  73.         /// Adds a root node.
  74.         /// </summary>
  75.         /// <param name="i">Label of the new node.</param>
  76.         //public void Add1(int i)
  77.         //{
  78.         //    tn = treeView1.Nodes.Add(i.ToString());
  79.         //}
  80.  
  81.         ///// <summary>
  82.         ///// Adds a node under the current node.
  83.         ///// </summary>
  84.         ///// <param name="i">Label of the new node.</param>
  85.         //public void Add2(int i)
  86.         //{
  87.         //    tn.Nodes.Add(i.ToString());
  88.         //    treeView1.ExpandAll();
  89.         //}
  90.  
  91.         /// <summary>
  92.         /// The filling thread's starting point
  93.         /// </summary>
  94.         private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
  95.         {
  96.             DirectoryInfo dir = new DirectoryInfo(@"\\server\trabajos 2013\");
  97.             Addnode(dir);
  98.         }
  99.  
  100.         /// <summary>
  101.         /// When form is loading
  102.         /// </summary>
  103.         private void Form1_Load(object sender, EventArgs e)
  104.         {
  105.             //Start filling the TreeView on a separate thread
  106.             backgroundWorker1.RunWorkerAsync();
  107.         }
  108.     }
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement