Export (0) Print
Expand All

TreeView.ImageList Property

Gets or sets the ImageList that contains the Image objects that are used by the tree nodes.

Namespace:  System.Windows.Forms
Assembly:  System.Windows.Forms (in System.Windows.Forms.dll)

public ImageList ImageList { get; set; }

Property Value

Type: System.Windows.Forms.ImageList
The ImageList that contains the Image objects that are used by the tree nodes. The default value is null.

If the ImageList property value is anything other than null, all the tree nodes display the first Image stored in the ImageList. You can specify which images from the list are displayed for selected and unselected nodes by setting the ImageIndex and SelectedImageIndex properties. Individual TreeNode objects can specify which image is displayed by setting their ImageIndex and SelectedImageIndex properties. These individual settings will override the settings in the corresponding TreeView properties.

NoteNote

In the .NET Framework version 1.1, there is an issue that prevents images from appearing on TreeView nodes when your application calls Application.EnableVisualStyles. To work around this issue, call Application.DoEvents in your Main method immediately after you call EnableVisualStyles. This issue is fixed in the .NET Framework 2.0.

The following code example creates and assigns an ImageList to a TreeView control and fills the TreeView control with TreeNode objects. The tree nodes are assigned images from the ImageList that are displayed when in a selected or unselected state. This example requires that you have a Form that contains a TreeView, and an ArrayList that contains Customer objects that each contain Order objects.


public class Customer
{
   public ArrayList CustomerOrders;
   public string CustomerName;
   public Customer(string myName)
   {
      CustomerName = myName;
      CustomerOrders = new ArrayList(); 
   }
}
public class Order
{
   public string OrderID;
   public Order(string myOrderID )
   {
      this.OrderID = myOrderID;
   }
}

private void FillTreeView()
{
	// Load the images in an ImageList.
	ImageList myImageList = new ImageList();
	myImageList.Images.Add(Image.FromFile("Default.gif"));
	myImageList.Images.Add(Image.FromFile("SelectedDefault.gif"));
	myImageList.Images.Add(Image.FromFile("Root.gif"));
	myImageList.Images.Add(Image.FromFile("UnselectedCustomer.gif"));
	myImageList.Images.Add(Image.FromFile("SelectedCustomer.gif"));
	myImageList.Images.Add(Image.FromFile("UnselectedOrder.gif"));
	myImageList.Images.Add(Image.FromFile("SelectedOrder.gif"));
	
	// Assign the ImageList to the TreeView.
	myTreeView.ImageList = myImageList;
	
	// Set the TreeView control's default image and selected image indexes.
	myTreeView.ImageIndex = 0;
	myTreeView.SelectedImageIndex = 1;

	/* Set the index of image from the 
	ImageList for selected and unselected tree nodes.*/ 
	this.rootImageIndex = 2;
	this.selectedCustomerImageIndex = 3;
	this.unselectedCustomerImageIndex = 4;
	this.selectedOrderImageIndex = 5;
	this.unselectedOrderImageIndex = 6;
	
	// Create the root tree node.
	TreeNode rootNode = new TreeNode("CustomerList");
	rootNode.ImageIndex = rootImageIndex;
	rootNode.SelectedImageIndex = rootImageIndex;

	// Add a main root tree node.
	myTreeView.Nodes.Add(rootNode);

	// Add a root tree node for each Customer object in the ArrayList. 
	foreach(Customer myCustomer in customerArray)
	{
		// Add a child tree node for each Order object. 
		int countIndex=0;
		TreeNode[] myTreeNodeArray = new TreeNode[myCustomer.CustomerOrders.Count];
		foreach(Order myOrder in myCustomer.CustomerOrders)
		{
			// Add the Order tree node to the array.
			myTreeNodeArray[countIndex] = new TreeNode(myOrder.OrderID,
			  unselectedOrderImageIndex, selectedOrderImageIndex);
			countIndex++;
		}
		// Add the Customer tree node.
		TreeNode customerNode = new TreeNode(myCustomer.CustomerName,
			unselectedCustomerImageIndex, selectedCustomerImageIndex, myTreeNodeArray);
		myTreeView.Nodes[0].Nodes.Add(customerNode);
	}
}

.NET Framework

Supported in: 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Show:
© 2014 Microsoft