title: Cannot set icon for node of treeview
body: <p>I have a treeview and a imageList which contain 1 icon (folder.ico), I want to set icon only for root node, child node will don't have icon, so I try to set image index for child node but it have some problem, look the picture:
  <img src="https://i.stack.imgur.com/znZ6C.png" alt="enter image description here"></p>
  
  <p>My code:</p>
  
  <pre><code>        ImageList imageList = new ImageList();
          imageList.Images.Add(Image.FromFile(System.AppDomain.CurrentDomain.BaseDirectory.Replace("\\bin\\Debug\\","") + "\\Images\\folder.ico"), Color.Transparent);
          treeView1.ImageList = imageList;
          foreach (TreeNode node in treeView1.Nodes)
          {
              foreach (TreeNode node2 in node.Nodes)
              {
                  node2.ImageIndex = 100;
                  node2.SelectedImageIndex = 100;
              }
          }
  </code></pre>
  
  <p>Thanks</p>
  
  <p><strong>EDIT</strong>
  I create a Custom TreeView at the answer of @Killercam:</p>
  
  <pre><code>class CustomTreeView : TreeView
  {
  public const int NOIMAGE = -1;
  
  public CustomTreeView()
      : base()
  {
      // .NET Bug: Unless LineColor is set, Win32 treeview returns -1 (default), .NET returns Color.Black!
      base.LineColor = SystemColors.GrayText;
      base.DrawMode = TreeViewDrawMode.OwnerDrawAll;
  }
  
  protected override void OnDrawNode(DrawTreeNodeEventArgs e)
  {
      // Space between Image and Label.
      const int SPACE_IL = 3;  
  
      // We only do additional drawing.
      e.DrawDefault = true;
      base.OnDrawNode(e);
      if (base.ShowLines &amp;&amp; base.ImageList != null &amp;&amp; e.Node.ImageIndex == NOIMAGE
          // exclude root nodes, if root lines are disabled
          //&amp;&amp; (base.ShowRootLines || e.Node.Level &gt; 0))
              )
      {
          // Using lines &amp; images, but this node has none: fill up missing treelines
  
          // Image size
          int imgW = base.ImageList.ImageSize.Width;
          int imgH = base.ImageList.ImageSize.Height;
  
          // Image center
          int xPos = e.Node.Bounds.Left - SPACE_IL - imgW / 2;
          int yPos = (e.Node.Bounds.Top + e.Node.Bounds.Bottom) / 2;
  
          // Image rect
          Rectangle imgRect = new Rectangle(xPos, yPos, 0, 0);
          imgRect.Inflate(imgW / 2, imgH / 2);
  
          using (Pen p = new Pen(base.LineColor, 1))
          {
              p.DashStyle = DashStyle.Dot;
  
              // Account uneven Indent for both lines.
              p.DashOffset = base.Indent % 2;
  
              // Horizontal treeline across width of image
              // account uneven half of delta ItemHeight &amp; ImageHeight.
              int yHor = yPos + ((base.ItemHeight - imgRect.Height) / 2) % 2;
  
              //if (base.ShowRootLines || e.Node.Level &gt; 0)
              //{
              //    e.Graphics.DrawLine(p, imgRect.Left, yHor, imgRect.Right, yHor);
              //}
              //else
              //{
              //    // for root nodes, if root lines are disabled, start at center
              //    e.Graphics.DrawLine(p, xPos - (int)p.DashOffset, yHor, imgRect.Right, yHor);
              //}
  
              e.Graphics.DrawLine(p,
                      (base.ShowRootLines || e.Node.Level &gt; 0) ? imgRect.Left : xPos - (int)p.DashOffset,
                      yHor, imgRect.Right, yHor);
              if (!base.CheckBoxes &amp;&amp; e.Node.IsExpanded)
              {
                  // Vertical treeline , offspring from NodeImage center to e.Node.Bounds.Bottom
                  // yStartPos: account uneven Indent and uneven half of delta ItemHeight &amp; ImageHeight
                  int yVer = yHor + (int)p.DashOffset;
                  e.Graphics.DrawLine(p, xPos, yVer, xPos, e.Node.Bounds.Bottom);
              }
          }
      }
  }
  
  protected override void OnAfterCollapse(TreeViewEventArgs e)
  {
      base.OnAfterCollapse(e);
      if (!base.CheckBoxes &amp;&amp; base.ImageList != null &amp;&amp; e.Node.ImageIndex == NOIMAGE)
      {
          // DrawNode event not raised: redraw node with collapsed treeline
          base.Invalidate(e.Node.Bounds);
      }
  }
  }
  </code></pre>
  
  <p>Then use it in my code:</p>
  
  <pre><code>private void TestCustomTreeView_Load(object sender, EventArgs e)
      {
          // @Killercam EDIT: Set the default Image to one that is not used.
          valForm.siteTreeView.ImageIndex = 100;
          valForm.siteTreeView.SelectedImageIndex = 100;
  
          TemplateCustomTreeView myTree = new TemplateCustomTreeView();
          myTree.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
  
          myTree.Location = new System.Drawing.Point(6, 26);
          myTree.Name = "tree";
          myTree.Scrollable = true;
          myTree.Size = new System.Drawing.Size(320, 296);
          myTree.ImageList = imageList1;
          /*Add item*/
          TreeNode node = new TreeNode();
          node.Name = "abc1";
          node.Text = "abc1";
          node.ImageIndex = 0;
          myTree.Nodes.Add(node);
          TreeNode node3 = new TreeNode();
          node3.Name = "abc2";
          node3.Text = "abc2";
          node3.ImageIndex = -1;            
          node.Nodes.Add(node3);
          ////
          TreeNode node2 = new TreeNode();
          node2.Name = "abc3";
          node2.Text = "abc3";
          node2.ImageIndex = 0;
          myTree.Nodes.Add(node2);
          this.Controls.AddRange(new System.Windows.Forms.Control[] { myTree });
  
      }
  }
  </code></pre>
  
  <p>The result still not work, It's still have a folder icon before the text!
  <img src="https://i.stack.imgur.com/1SwB0.png" alt="enter image description here"></p>