This topic has not yet been rated - Rate this topic

TreeNode.ImageIndex Property

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

Namespace:  System.Windows.Forms
Assembly:  System.Windows.Forms (in System.Windows.Forms.dll)
'Declaration
<RelatedImageListAttribute("TreeView.ImageList")> _
<TypeConverterAttribute(GetType(TreeViewImageIndexConverter))> _
Public Property ImageIndex As Integer

Property Value

Type: System.Int32
A zero-based index value that represents the image position in the assigned ImageList.

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

NoteNote

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

The ImageKey and ImageIndex properties are mutually exclusive; therefore, if one property is set, the other is ignored. If you set the ImageKey property, the ImageIndex property is automatically set to -1. Alternatively, if you set ImageIndex, ImageKey is automatically set to an empty string ("").

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 is displayed when the tree node is 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. It also requires that the Customer and Order objects are defined.

Public Class Customer
   Public CustomerOrders As ArrayList
   Public CustomerName As String 

   Public Sub New(myName As String)
      CustomerName = myName
      CustomerOrders = New ArrayList()
   End Sub 'New 
End Class 'Customer


Public Class Order
   Public OrderID As String 

   Public Sub New(myOrderID As String)
      Me.OrderID = myOrderID
   End Sub 'New 
End Class 'Order

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

.NET Framework

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

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows Phone 8.1, Windows Phone 8, 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.

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.