title: Deselect TreeView Node
body: <p>I do a lot of modifications in Windows so I constantly visit this and that folder. Doing this manually is a pain. That's why I thought about creating a small tool which will contain some of the environment variables.</p>
  
  <p><a href="https://i.stack.imgur.com/z0WYQ.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/z0WYQ.png" alt="screenshot of the tool"></a></p>
  
  <p>I have a TreeView. I select a node and the tool opens the corresponding folder.</p>
  
  <pre><code>public static class EnvPaths {
      public static string HomeDrive = Environment.GetEnvironmentVariable("HomeDrive");
      public static string ProgramFiles = Environment.GetEnvironmentVariable("ProgramFiles");
      public static string CommonFiles = Environment.GetEnvironmentVariable("CommonProgramFiles");
      public static string ProgramFilesX86 = Environment.GetEnvironmentVariable("ProgramFiles(x86)");
      public static string CommonFilesX86 = Environment.GetEnvironmentVariable("CommonProgramFiles(x86)");
      public static string ProgramData = Environment.GetEnvironmentVariable("ProgramData");
      public static string User = Environment.GetEnvironmentVariable("HomePath");
      public static string AppData = Environment.GetEnvironmentVariable("AppData");
      public static string QuickLaunch = AppData + @"\Microsoft\Internet Explorer\Quick Launch";
      public static string Temp = Environment.GetEnvironmentVariable("Temp");
      public static string Windows = Environment.GetEnvironmentVariable("WinDir");
      public static string System32 = Windows + @"\system32";
      public static string etc = System32 + @"\drivers\etc";
  }
  
  private void WinFoldersTreeView_AfterSelect(object sender, TreeViewEventArgs e)
  {
      if (WinFoldersTreeView.SelectedNode != null)
      {
          var selectedFolder = WinFoldersTreeView.SelectedNode.Text;
          switch (selectedFolder)
          {
              case "Home Drive":
                  Process.Start(EnvPaths.HomeDrive);
                  break;
              case "Program Files":
                  Process.Start(EnvPaths.ProgramFiles);
                  break;
              case "Common Files":
                  Process.Start(EnvPaths.CommonFiles);
                  break;
              case "Program Files (x86)":
                  Process.Start(EnvPaths.ProgramFilesX86);
                  break;
              case "Common Files (x86)":
                  Process.Start(EnvPaths.CommonFilesX86);
                  break;
              case "ProgramData":
                  Process.Start(EnvPaths.ProgramData);
                  break;
              case "User":
                  Process.Start(EnvPaths.User);
                  break;
              case "AppData":
                  Process.Start(EnvPaths.AppData);
                  break;
              case "Quick Launch":
                  Process.Start(EnvPaths.QuickLaunch);
                  break;
              case "Temp":
                  Process.Start(EnvPaths.Temp);
                  break;
              case "Windows":
                  Process.Start(EnvPaths.Windows);
                  break;
              case "System32":
                  Process.Start(EnvPaths.System32);
                  break;
              case "etc":
                  Process.Start(EnvPaths.etc);
                  break;
          }
      }
  }
  </code></pre>
  
  <p>I have a problem though. Once it opens the folder, I want to deselect the selected node. So if I open a folder, close it, and want to open it again, I can't. I can't select the selected node again. I need to select another node, and then select the original node again. Which will open two folders, not one.</p>
  
  <p>I've used <code>WinFoldersTreeView.SelectedNode = null</code> to deselect the node. This works but causes another problem. Since the TreeView has its <code>AfterSelect</code> event attached, setting <code>WinFoldersTreeView.SelectedNode</code> to <code>null</code> triggers the <code>AfterSelect</code> event again. I end up opening two folders.</p>
  
  <ul>
  <li>I select "Program Files"</li>
  <li><code>Process.Start(EnvPaths.ProgramFiles);</code> opens the folder.</li>
  <li><code>WinFoldersTreeView.SelectedNode = null;</code> deselects the selected node and triggers <code>AfterSelect</code> event again. This also sets the first node ("Home Drive") as the selected node.</li>
  <li>I close "Program Files" folder (the second <code>AfterSelect</code> event is in progress) and the "Home Drive" (C:) folder opens.</li>
  </ul>
  
  <p>How can I deselect the selected node without triggering the <code>AfterSelect</code> event?</p>