• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1389
  • 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
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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