How to: Set Icons for the Windows Forms TreeView Control

The Windows Forms TreeView control can display icons next to each node. The icons are positioned to the immediate left of the node text. To display these icons, you must associate the tree view with an ImageList control. For more information about image lists, see ImageList Component and How to: Add or Remove Images with the Windows Forms ImageList Component.

Note

A bug in Microsoft .NET Framework version 1.1 prevents images from appearing on TreeView nodes when your application calls Application.EnableVisualStyles. To work around this bug, call Application.DoEvents in your Main method immediately after calling EnableVisualStyles. This bug is fixed in .NET Framework 2.0.

To display images in a tree view

  1. Set the TreeView control's ImageList property to the existing ImageList control you wish to use.

    These properties can be set in the designer with the Properties window, or in code.

    TreeView1.ImageList = ImageList1  
    
    treeView1.ImageList = imageList1;  
    
    treeView1->ImageList = imageList1;  
    
  2. Set the node's ImageIndex and SelectedImageIndex properties. The ImageIndex property determines the image displayed for the node's normal and expanded states, and the SelectedImageIndex property determines the image displayed for the node's selected state.

    These properties can be set in code, or within the TreeNode Editor. To open the TreeNode Editor, click the ellipsis button ( The Ellipsis button (...) in the Properties window of Visual Studio.) next to the Nodes property on the Properties window.

    ' (Assumes that ImageList1 contains at least two images and  
    ' the TreeView control contains a selected image.)  
    TreeView1.SelectedNode.ImageIndex = 0  
    TreeView1.SelectedNode.SelectedImageIndex = 1  
    
    // (Assumes that imageList1 contains at least two images and  
    // the TreeView control contains a selected image.)  
    treeView1.SelectedNode.ImageIndex = 0;  
    treeView1.SelectedNode.SelectedImageIndex = 1;  
    
    // (Assumes that imageList1 contains at least two images and  
    // the TreeView control contains a selected image.)  
    treeView1->SelectedNode->ImageIndex = 0;  
    treeView1->SelectedNode->SelectedImageIndex = 1;  
    

See also