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 (Windows Forms) and How to: Add or Remove Images with the Windows Forms ImageList Component.
Poznámka
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
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.set_ImageList(imageList1);
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.
' (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.get_SelectedNode().set_ImageIndex(0); treeView1.get_SelectedNode().set_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
Tasks
How to: Add and Remove Nodes with the Windows Forms TreeView Control
How to: Iterate Through All Nodes of a Windows Forms TreeView Control
How to: Determine Which TreeView Node Was Clicked (Windows Forms)
How to: Add Custom Information to a TreeView or ListView Control (Windows Forms)