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)

Public Property SelectedImageIndex As Integer

Property Value

Type: System.Int32

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

Exception Condition

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.


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, except for 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 that contains a TreeView, and an ArrayList that contains Customer objects that each contain Order objects. It is also assumed 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 'NewNew
End Class 'Customer

Public Class Order
   Public OrderID As String

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

Private Sub FillTreeView()
   ' Load the images in an ImageList.
   Dim myImageList As New ImageList()

   ' 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.

   ' 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)
   Next myCustomer
End Sub

.NET Framework
Available since 1.1
Return to top