Export (0) Print
Expand All

TreeNode.ImageIndex Property

Gets or sets the image list index value of the image displayed when the tree node is in the unselected state.

[Visual Basic]
Public Property ImageIndex As Integer
[C#]
public int ImageIndex {get; set;}
[C++]
public: __property int get_ImageIndex();
public: __property void set_ImageIndex(int);
[JScript]
public function get ImageIndex() : int;
public function set ImageIndex(int);

Property Value

A zero-based index value that represents the image position in the assigned ImageList.

Remarks

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

Note   The default value of the ImageIndex property is the same as the ImageIndex property of the TreeView control that the TreeNode is assigned to.

Example

[Visual Basic, C#, C++] The following 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 seleced or unselected state. This example assumes 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.

[Visual Basic] 
Private Sub FillTreeView()
   ' Load the images in an ImageList.
   Dim myImageList As 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.
   Me.rootImageIndex = 2
   Me.selectedCustomerImageIndex = 3
   Me.unselectedCustomerImageIndex = 4
   Me.selectedOrderImageIndex = 5
   Me.unselectedOrderImageIndex = 6
   
   ' Create the root tree node.
   Dim rootNode As 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.
   Dim myCustomer As Customer
   For Each myCustomer In  customerArray
      ' Add a child tree node for each Order object.
      Dim countIndex As Integer = 0
      Dim myTreeNodeArray(myCustomer.CustomerOrders.Count) As TreeNode
      Dim myOrder As Order
      For Each myOrder In  myCustomer.CustomerOrders
         ' Add the Order tree node to the array.
         myTreeNodeArray(countIndex) = New TreeNode(myOrder.OrderID, _
            unselectedOrderImageIndex, selectedOrderImageIndex)
         countIndex += 1
      Next myOrder
      ' Add the Customer tree node.
      Dim customerNode As New TreeNode(myCustomer.CustomerName, _
         unselectedCustomerImageIndex, selectedCustomerImageIndex, myTreeNodeArray)
      myTreeView.Nodes(0).Nodes.Add(customerNode)
   Next myCustomer
End Sub

[C#] 
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);
    }
}

[C++] 
private:
    void FillTreeView() {
        // Load the images in an ImageList.
        ImageList* myImageList = new ImageList();
        myImageList->Images->Add(Image::FromFile(S"Default.gif"));
        myImageList->Images->Add(Image::FromFile(S"SelectedDefault.gif"));
        myImageList->Images->Add(Image::FromFile(S"Root.gif"));
        myImageList->Images->Add(Image::FromFile(S"UnselectedCustomer.gif"));
        myImageList->Images->Add(Image::FromFile(S"SelectedCustomer.gif"));
        myImageList->Images->Add(Image::FromFile(S"UnselectedOrder.gif"));
        myImageList->Images->Add(Image::FromFile(S"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(S"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.
        IEnumerator* myEnum = customerArray->GetEnumerator();
        while (myEnum->MoveNext()) {
            Customer* myCustomer = __try_cast<Customer*>(myEnum->Current);

            // Add a child tree node for each Order object.
            int countIndex=0;
            TreeNode* myTreeNodeArray[] = new TreeNode*[myCustomer->CustomerOrders->Count];
            IEnumerator* myEnum = myCustomer->CustomerOrders->GetEnumerator();
            while (myEnum->MoveNext()) {
                Order* myOrder = __try_cast<Order*>(myEnum->Current);

                // Add the Order tree node to the array.
                myTreeNodeArray->Item[countIndex] = new TreeNode(myOrder->OrderID,
                    unselectedOrderImageIndex, selectedOrderImageIndex);
                countIndex++;
            }
            // Add the Customer tree node.
            TreeNode* customerNode = new TreeNode(myCustomer->CustomerName,
                unselectedCustomerImageIndex, selectedCustomerImageIndex, myTreeNodeArray);
            myTreeView->Nodes->Item[0]->Nodes->Add(customerNode);
        }
    }

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework

See Also

TreeNode Class | TreeNode Members | System.Windows.Forms Namespace | ImageList | TreeView.ImageList | SelectedImageIndex

Show:
© 2014 Microsoft