title: How to programmatically expand a Treeview?
body: <p>I wrote the following code to iterate my <code>Treeview</code> and find the full path given.</p>
  
  <p>I get the correct position and the <code>listview</code> also shows the correct content, <strong>but the <code>treeview</code> is not expanded.</strong> It seems that the <code>.Expand()</code> method does not firing.</p>
  
  <p>What could that be about?</p>
  
  <p>The Form with the <code>Treeview</code> is a Dialog, which is initialized once at the program-start and then is refreshed and shown when needed.</p>
  
  <p>Because of the huge number of folders into the deep, I just load 2 levels of folders downward with each manually expanded.</p>
  
  <p>Before I show the Dialog again, I set the <code>SelectedPath</code> to the full path which should be expanded and than calling the <code>ExpandToPath</code> method.</p>
  
  <pre><code>    private void treeView1_BeforeExpand(object sender, System.Windows.Forms.TreeViewCancelEventArgs e) {
          if(e.Node.Tag != null &amp;&amp; e.Node.Nodes.Count &gt; 0)
              PopulateTreeView(e.Node, 1);
      }
  
  
  
      private void PopulateTreeView(TreeNode node, int depth) {
  
          if(node.Tag != null) {
  
              TreeViewTag tag = node.Tag as TreeViewTag;
              if(tag.Visited &amp;&amp; node.Nodes.Count &gt; 0) {
  
                  foreach(TreeNode n in node.Nodes) {
                      if(depth &lt;= 1)
                          PopulateTreeView(n, depth + 1);
                  }
  
              } else {
  
                  TreeNode aNode;
                  try {
                      DirectoryInfo[] infos = tag.Info.GetDirectories();
  
                      foreach(DirectoryInfo subDir in infos) {
                          try {
                              aNode = new TreeNode(subDir.Name, 0, 0);
                              aNode.Tag = new TreeViewTag() { Info = subDir, Visited = true };
                              aNode.ImageKey = "folderOpen";
  
                              if(depth &lt;= 1)
                                  PopulateTreeView(aNode, depth + 1);
  
                              node.Nodes.Add(aNode);
                          } catch(UnauthorizedAccessException ex11) {
                          } catch(Exception ex12) {
                          }
  
                      }
                  } catch(UnauthorizedAccessException ex1) {
                  } catch(Exception ex2) {
                  }
              }
          }
      }
  
  
      public void ExpandToPath(string path) {
          currentNodeInfo = new DirectoryInfo(path);
  
          TreeNode root = this.treeView1.Nodes[0];
          if(path.EndsWith(Path.DirectorySeparatorChar.ToString()))
              path = path.Substring(0, path.Length - 1);
  
          Stack&lt;TreeNode&gt; search = new Stack&lt;TreeNode&gt;();
          search.Push(root);
  
          while(search.Count &gt; 0) {
              TreeNode current = search.Pop();
              string compare = (current.Tag as TreeViewTag).Info.FullName;
              if(path.Equals(compare)) {
                  current.Expand();
                  PopulateListView((current.Tag as TreeViewTag).Info);
                  break;
              } else {
                  if(path.StartsWith(compare)) {
                      current.Expand();
                      foreach(TreeNode node in current.Nodes) {
                          search.Push(node);
                      }
                  }
              }
          }
          search.Clear();
          search = null;
      }
  </code></pre>