• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1254
  • Last Modified:

Using ImageList in TreeView in Excel VBA

Hi,
I would like to use images from my imagelist to show the same ones in treeView....
I loaded images using custom options in properties for the imagelist....
There are 3 images currently in the image list...
Below is my VBA code for populating treeView....
What should I do in order to use image index to show picture next to my treeview nodes...

Please help...
Thanks in advance...

regards.
Public Sub AddNodes(strSql As String, oTV As Object, oImages As Object, mode As Boolean)
'-----------------


If cnn.State = 1 Then
cnn.Close
End If

stConn = CConstants.GetSQLConnection
cnn.Open stConn
cnn.CursorLocation = adUseClient



Dim myTree As TreeView

    Set myTree = oTV
   
    ' clear all nodes
    myTree.Nodes.Clear
    


        rs.Open strSql, cnn

    With rs
      If Not (.EOF And .BOF) Then
         Do Until .EOF
            If IsNull(.Fields("Reports")) Then
               
               If mode = True Then
               strKey = "a" & .Fields("ID")
               strText = .Fields("Description")
               nodeTag = Nz(.Fields("nodeTag"), "")
               myTree.Nodes.Add , , strKey, strText
               Else
               strKey = "a" & .Fields("ID")
               strText = RTrim(.Fields("ID") + " * " + .Fields("Name"))
               nodeTag = .Fields("MaterializedPath")
                myTree.Nodes.Add , , strKey, strText 
               End If
               
               
               
            Else
               strRelative = "a" & .Fields("Reports")
                              
               If mode = True Then
               
               strKey = "a" & .Fields("ID")
               strText = RTrim(.Fields("Description") + "" + Nz(.Fields("Description2"), ""))
               nodeTag = Nz(.Fields("nodeTag"), "")
               Else
               strKey = "a" & .Fields("ID")
               strText = RTrim(.Fields("ID") + " * " + .Fields("Name"))
               nodeTag = .Fields("MaterializedPath")
                              End If
               
                 myTree.Nodes.Add strRelative, tvwChild, strKey, strText 
                

               
           If mode = True Then
               If Len(.Fields("nodeTag") & "") > 0 Then
                  myTree.Nodes(strKey).Tag = .Fields("nodeTag")
               End If
           Else
               If Len(.Fields("MaterializedPath") & "") > 0 Then
                  myTree.Nodes(strKey).Tag = .Fields("MaterializedPath")
               End If
            End If

            End If
            .MoveNext
         Loop
      End If
   End With

   rs.Close

   Set cnn = Nothing
   Set rs = Nothing
   


End Sub

Open in new window

0
alsam
Asked:
alsam
  • 4
  • 4
1 Solution
 
Rory ArchibaldCommented:
You need to set the Imagelist property of the treeview control to your ImageList control, then the 5th argument of the Nodes.Add method is the index number of the picture you want to use for that node.
0
 
alsamAuthor Commented:
Hi,
would be easy if I use .net or something else...But I can not find ImageList property in TreeView properties to set as you suggested...So I tried to do it programatically


Private Sub UserForm_Initialize()
Set Me.BranchTV.ImageList = Me.BranchImageList
End Sub

it says that the ImageList has to be initialized...

Do you have any suggestion for me how to make this working...
0
 
Rory ArchibaldCommented:
You are using an imagelist from the Microsoft Windows Common Controls library? Do you have a reference set to that library? If so, it should work.
0
Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

 
alsamAuthor Commented:
Yes I do...but does not work...It's question mark:(
0
 
Rory ArchibaldCommented:
What do you mean by "It's question mark"?
0
 
alsamAuthor Commented:
I means it's something that confusses me like it's checked but does not work ....just ignore the "It's question mark" from my post....it's our local sleng...


Microsoft Windows Common Controls is checked in references....but it still does not work

Sorry for confussion..
0
 
Rory ArchibaldCommented:
Could you post a workbook with the userform in it as you have it now?
0
 
alsamAuthor Commented:
Hi rorya,
Sorry for the delay and thank you for your time and effort....
Actually, I solved problem by my self
0

Featured Post

Free recovery tool for Microsoft Active Directory

Veeam Explorer for Microsoft Active Directory provides fast and reliable object-level recovery for Active Directory from a single-pass, agentless backup or storage snapshot — without the need to restore an entire virtual machine or use third-party tools.

  • 4
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now