Dong Lam Trien

Dong Lam Trien

  • 829
  • 986
  • 149.2k

How to set icons for the windows forms treeview control ?

Mar 26 2020 11:42 PM
I use imageList to store icons and I assign imageList to treeView, I use treeView1_DrawNode to change icon for treeView but this function does not run. See my photo and code below.
  1. namespace TreeView  
  2. {  
  3. public partial class Form1 : Form  
  4. {  
  5. public Form1()  
  6. {  
  7. InitializeComponent();  
  8. treeView1.ImageList = imageList1;  
  9. this.treeView1.DrawNode += new System.Windows.Forms.DrawTreeNodeEventHandler(this.treeView1_DrawNode);  
  10. dt = CreateTable(10);  
  11. CreateTreeView(dt, treeView1, true);  
  12. }  
  13. DataTable dt = new DataTable();  
  14. ... //My code  
  15. private void treeView1_DrawNode(object sender, DrawTreeNodeEventArgs e)  
  16. {  
  17. if (e.Node.IsExpanded || e.Node.Nodes.Count < 1)  
  18. treeView1.ImageIndex = 0;  
  19. else  
  20. treeView1.ImageIndex = 1;  
  21. }  
  22. }  
http://www.mediafire.com/file/iz4mvxfu9o0h863/treeView01.jpg
 
Why is the treeView1_DrawNode(...) function not running? How to change my treeView icons instead of +,- like a windows explorer treeview in win forms?

Answers (4)