Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

TreeView.SelectedImageIndex Property

Gets or sets the image list index value of the image that is displayed when a tree node is selected.

Namespace: System.Windows.Forms
Assembly: System.Windows.Forms (in system.windows.forms.dll)

[LocalizableAttribute(true)] 
public int SelectedImageIndex { get; set; }
/** @property */
public int get_SelectedImageIndex ()

/** @property */
public void set_SelectedImageIndex (int value)

public function get SelectedImageIndex () : int

public function set SelectedImageIndex (value : int)

Property Value

A zero-based index value that represents the position of an Image in an ImageList.
Exception typeCondition

ArgumentException

The index assigned value is less than zero.

The SelectedImageIndex value is the index of an Image stored in the ImageList assigned to the ImageList property.

NoteNote

When setting the SelectedImageIndex property at run time, the TreeView handle is recreated (see Control.RecreateHandle) to update the control's appearance. This causes all tree nodes to be collapsed, with the exception of the selected TreeNode.

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 containing a TreeView, and an ArrayList containing Customer objects that each contain Order objects. It is also assumed that the Customer and Order objects are defined.

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);
	}
}

private void FillTreeView()
{
    // Load the images in an ImageList.
    ImageList myImageList = new ImageList();
    myImageList.get_Images().Add(Image.FromFile("Default.gif"));
    myImageList.get_Images().Add(Image.FromFile("SelectedDefault.gif"));
    myImageList.get_Images().Add(Image.FromFile("Root.gif"));
    myImageList.get_Images().Add(Image.FromFile("UnselectedCustomer.gif"));
    myImageList.get_Images().Add(Image.FromFile("SelectedCustomer.gif"));
    myImageList.get_Images().Add(Image.FromFile("UnselectedOrder.gif"));
    myImageList.get_Images().Add(Image.FromFile("SelectedOrder.gif"));
    // Assign the ImageList to the TreeView.
    myTreeView.set_ImageList(myImageList);
    // Set the TreeView control's default image and selected image indexes.
    myTreeView.set_ImageIndex(0);
    myTreeView.set_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.set_ImageIndex(rootImageIndex);
    rootNode.set_SelectedImageIndex(rootImageIndex);
    // Add a main root tree node.
    myTreeView.get_Nodes().Add(rootNode);
    // Add a root tree node for each Customer object in the ArrayList.
    for (int iCtr1 = 0; iCtr1 < customerArray.get_Count(); iCtr1++) {
        Customer myCustomer = (Customer)customerArray.get_Item(iCtr1);
        // Add a child tree node for each Order object.
        int countIndex = 0;
        TreeNode myTreeNodeArray[] =
            new TreeNode[myCustomer.customerOrders.get_Count()];
        for (int iCtr2 = 0; iCtr2 < myCustomer.customerOrders.get_Count();
            iCtr2++) {
            Order myOrder = (Order)myCustomer.customerOrders.get_Item(iCtr2);
            // Add the Order tree node to the array.
            myTreeNodeArray.set_Item(countIndex,
                new TreeNode(myOrder.orderID, unselectedOrderImageIndex,
                selectedOrderImageIndex));
            countIndex++;
        }
        // Add the Customer tree node.
        TreeNode customerNode = new TreeNode(myCustomer.customerName,
            unselectedCustomerImageIndex, selectedCustomerImageIndex,
            myTreeNodeArray);
        myTreeView.get_Nodes().get_Item(0).get_Nodes().Add(customerNode);
    }
} //FillTreeView

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

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

.NET Framework

Supported in: 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.