Setting 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 Introduction to the Windows Forms ImageList Component and Adding or Removing Images with the Windows Forms ImageList Component.
To display images in a tree view
- 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.
' Visual Basic TreeView1.ImageList = ImageList1 // C# treeView1.ImageList = imageList1; // C++ treeView1->ImageList = imageList1;
- 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 () next to the Nodes property on the Properties window.
' Visual Basic ' (Assumes that ImageList1 contains at least two images and ' the TreeView control contains a selected image.) TreeView1.SelectedNode.ImageIndex = 0 TreeView1.SelectedNode.SelectedImageIndex = 1 // C# // (Assumes that imageList1 contains at least two images and // the TreeView control contains a selected image.) treeView1.SelectedNode.ImageIndex = 0; treeView1.SelectedNode.SelectedImageIndex = 1; // C++ // (Assumes that imageList1 contains at least two images and // the TreeView control contains a selected image.) treeView1->SelectedNode->ImageIndex = 0; treeView1->SelectedNode->SelectedImageIndex = 1;
Introduction to the Windows Forms TreeView Control | Adding and Removing Nodes with the Windows Forms TreeView Control | Iterating Through All Nodes of a Windows Forms TreeView Control | Determining Which TreeView Node Was Clicked (Windows Forms) | Adding Custom Information to a TreeView or ListView Control (Windows Forms)