how do I use Treeview Click event to show pictures in folders

Maazter

Registered User.
Local time
Today, 02:55
Joined
Nov 25, 2014
Messages
28
Hi eveyone,

I have been looking into populating a tree-view with folders and files, and have found some great examples for doing this, How ever...I cannot seem to work out how to show files/pictures in a form based on the click event of the tree-view.

I have managed to do an onclick event for the files in the tree-view, but I cannot seem to get the onclick event for folders in a tree-view.

Can anyone show me an example so I can learn some more about the treeview, still a newbie but trying hard to learn :)
Here is an example which I am working on at the moment, but get the error Object variable Or with block variable not set

Code:
Private Sub tvw_Click()

   Dim tClient As String
   Dim tParent As Scripting.folder
   Dim fd As Scripting.folder
   Dim fNode As MSComctlLib.Node
   Dim nFolder As String
   Dim fso As New Scripting.FileSystemObject
   
   tClient = Me.Klantnaam
   
   Set tParent = fso.GetFolder(fNode.Key) ' <Object variable Or with block variable not set
   
   For Each fd In tParent.SubFolders

      nFolder = fNode.Key & "\" & fd.Name
      

   Next fd
   
   Forms!Addpics!FilePathLbl = nFolder
   Call GetImagesTreeview(nFolder, tClient)

End Sub



Thank you in advance of any answer.


Mark
 
Last edited:
You've declared fNode, but you never assign it a value. You can't use it until it 'contains' a valid Node object.

A more common event handler, when you use the treeview, is NodeClick, which provides the clicked node . . .

Code:
Private Sub m_tvw_NodeClick(ByVal Node As MSComctlLib.Node)
    MsgBox "The key of the node you clicked is " & Node.Key
End Sub
 
Hi Mark,

Thank you for your reply.

Yea i noticed there was another function and I have all ready been busy with the code, so far this is what i have got.

Code:
Private Sub tvw_NodeClick(ByVal n1 As MSComctlLib.Node)
    Dim fso As New Scripting.FileSystemObject
    
        Dim tClient As String
        Dim tParent As Scripting.folder
        Dim tFolder As Scripting.folder
        Dim tFile As Scripting.File
        Dim nFolder, sFolder, fPath, pFolder, nFile, sFile As String


        
        tClient = Me.Klantnaam
        pFolder = sPath
             
        Set tParent = fso.GetFolder(sPath)
        
   For Each tFolder In tParent.SubFolders
           
            nFolder = n1.Key
            sFolder = tFolder.Name

            Next tFolder

   For Each tFile In tParent.Files

            nFile = n1.Key
            sFile = tFile.Name
            
            Next tFile
 
 
   If n1.Child.Selected = true Then 
   
           
  
              Forms!Addpics!FilePathLbl = nFile
              'Forms!Addpics!FilePathLbl.SetFocus
              'Me.ImgSingle.Picture = nFile
              
              'Tab1.Pages(0).SetFocus
              
              'ImageHide

End If

       If n1.Selected = True Then
       
      fPath = nFolder
      Call GetImagesTreeview(fPath, tClient)

     
Else

      
      Call GetImagesTreeview(sPath, tClient)

      
      
 End If
End Sub

all works fine appart from when i want to select a single picture/File it doesnt load it, how do I define a file node from a folder node? if this is the correct method of asking :D
 
select a single picture/File it doesnt load
What, exactly, should load? And under what exact circumstances?

how do I define a file node from a folder node?
I don't understand this question.
 
Hi Markk


Sorry for my layman questions :)

What I mean is...

At the moment if I click on one of the folder's/subfolder in my treeview, I get the pictures in that folder's/subfolder to load into a form via an Array.

But when i come to click on one of the individual nodes in the treeview it should normally show a single picture, but I cannot define between a folder node and a file node.

Attached is an example of my tree and the files inside the tree.

I hope this is more explanatory.
 

Attachments

  • Example.PNG
    Example.PNG
    3 KB · Views: 139
When you create your nodes, flag them somehow, maybe use the Tag property, or add a character to the key. Consider this code at node creation time . . .
Code:
set nd = tvw.nodes.add(np, tvwChild, Key, Text)
[COLOR="DarkRed"]nd.Tag = "jpg"[/COLOR]
Then, when you click a node . . .
Code:
Private Sub tvw_NodeClick(ByVal nc As MSComctlLib.Node)
    Select Case nc.Tag
        Case "file"
            'take action on file node
        Case "folder"
            'take action on folder node
        Case "jpg"
            'take action on picture
    End Select
End Sub
Does that help? Does that address your problem?
 
Hi Mark

Great thank you for your help, I will give it a go right away :-)

Thnx again
 
Hi Markk,

This what I have so far, but when i Click on the file or folder it does not do anything.


Code:
Private Sub CreateNodes(Node1 As MSComctlLib.Node)

   Dim fdParent As Scripting.folder
   Dim fd As Scripting.folder
   Dim fFile As Scripting.File
   Dim Node2 As MSComctlLib.Node
   Dim k As String
   Dim fso As New Scripting.FileSystemObject
   
   Set fdParent = fso.GetFolder(Node1.Key)
   
   For Each fd In fdParent.SubFolders

      k = Node1.Key & "\" & fd.Name
      Set Node2 = tvw.Nodes.Add(Node1.Key, tvwChild, k, fd.Name, 3)

      Node2.Tag = "fd"

      tvw.Nodes.Add k, tvwChild, "*" & k
      

   Next fd
   

   For Each fFile In fdParent.Files

      k = Node1.Key & "\" & fFile.Name

      Set Node2 = tvw.Nodes.Add(Node1.Key, tvwChild, k, fFile.Name, 4)
      
   Next fFile
   
      
   If Not Node1.Child Is Nothing Then Node1.Child.EnsureVisible
   
   Exit Sub
   
noDisk:
   Select Case Err
      Case 76  'path not found
         'ignore this error
      Case Else
         MsgBox Err & " " & Err.Description
   End Select
   
End Sub

Private Sub tvw_NodeClick(ByVal Node1 As MSComctlLib.Node)
    Dim fso As New Scripting.FileSystemObject
    
        Dim tClient As String
        Dim tParent As Scripting.folder
        Dim tFolder As Scripting.folder
        Dim tFile As Scripting.File
        Dim nFolder, sFolder, fPath, pFolder, nFile, sFile As String

        
        tClient = Me.Klantnaam
        pFolder = sPath
             
        Set tParent = fso.GetFolder(sPath)
        
        For Each tFolder In tParent.SubFolders
           
            nFolder = Node1.Key
            sFolder = tFolder.Name

        Next tFolder
            
        For Each tFile In tParent.Files

            nFile = Node1.Key
            sFile = tFile.Name
            
        Next tFile
        
Select Case Node1.Tag

        Case "file"
        
            'take action on file node
            
              Forms!Addpics!FilePathLbl = nFile
              Forms!Addpics!FilePathLbl.SetFocus
              Me.ImgSingle.Picture = nFile
              
              Tab1.Pages(0).SetFocus
              
              ImageHide

            
        Case "folder"
            'take action on folder node
            
      If Node1.Selected = True Then
       
      ImageShow
      fPath = nFolder
      Call GetImagesTreeview(fPath, tClient)

     
Else
      
      ImageShow
      Call GetImagesTreeview(sPath, tClient)



        End If
            

    End Select
        

End Sub
 
Here's your node creation code . . .
Code:
      Node2.Tag = "fd"
. . . and here's your node click code . . .
Code:
Select Case Node1.Tag
        Case "file"
        Case "folder"
End Select
Can you spot the problem?
 
OK, so you set the Tag to "fd", and when you test the Tag, you are testing it for?
 
Hi again Markk

Ok I think i am seeing a little bit what you are pointing too :-)

I changed the Node1.Tag = "fd" to Node1.Tag = Node2 and placed a msgbox after it, now instead of "fd" in the msgbox I get the subfolder names as a message.

not sure if this is correct but I understand that it didn't do much with Node1.Tag = "fd"

thank you for your time on this Mark, I am trying hard at this coding and your patience is well appreciated :-)
 
Well, you are very ambitious to try to use treeviews. They are not easy to code, as you know.
 
Hi Markk,

Thnx :-)

I m afraid I do not have a choice though, I had to change my career due to a back injury and programming was the only thing i Could do in my situation.

But I don't believe in quiting, it's not in my nature.

I previously had the form working perfectly using GetFilesandFolder & storing them into listboxes, this was effective until I needed to show more subfolders.

The problem of cause was having to create more and more listboxes, it became messy :-)

Now I want to get a grasp of the treeview because the options are much wider as you know.

I have been looking into the code further and until now all the functions are working ie double clicking the folders and files in the treeview.

I think it's just not seeing the Select Case.

Im going to try some debug with msgboxes and see if the case is getting called.

thnx again markk
 
Hi MarkK

I have done some debugging,

If I place Before the Select Case Node1.Tag

msgbox Node1.key

It passes the value on any folder/File that is clicked but after it reaches the first case it just stops.

Do I have to define the Case in Select Case in anyway?
 
OK, maybe I'll back-track and make sure I understand the problem. I think you want to handle node clicks and know which type of node was clicked, is that right? But the difference between nodes is only a difference in YOUR data. The nodes themselves are exactly the same. So in order to tell them apart when you click them, you need to flag them somehow when you create them. Does all this make sense so far?

Now, a node has a Tag property, which is a string. You can use this property to put hidden data into a node, so that later, when you click that node, your code can determine what data it represents.

So lets say after I create a node I do this, paying special attention to the line in red . . .
Code:
dim nd as mscomctllib.node
set nd = tvw.nodes.add(np.key, tvwChild, k, text)
[COLOR="DarkRed"]nd.Tag = "fd"[/COLOR]
Now, when I click that node, the NodeClick handler runs . . .
Code:
Private Sub tvw_NodeClick(ByVal nc As MSComctlLib.Node)
    MsgBox "This code runs for all node clicks. tag: " & nc.Tag & ", key: " & nc.key & ", text: " & nc.text
    Select Case [COLOR="DarkRed"]nc.Tag[/COLOR]
        [COLOR="DarkRed"]Case "fd"[/COLOR]
[COLOR="Green"]            'take action on nodes tagged "fd"[/COLOR]
            MsgBox "this code only runs for nodes tagged "fd"
        Case "folder"
[COLOR="Green"]            'take action on folder nodes[/COLOR]
        Case "jpg"
[COLOR="Green"]            'take action on pictures[/COLOR]
    End Select
End Sub
. . . and you can see in the Select Case block how you tell your nodes apart by tag, see the code in red.

This way you can do different things based on the type of node that was clicked.

Does that make things clearer?
 
Hi again MarkK,

Made a work around for now with on error 52.

Code:
Private Sub tvw_NodeClick(ByVal Node1 As MSComctlLib.Node)
    Dim fso As New Scripting.FileSystemObject
    
        Dim tClient As String
        Dim tParent As Scripting.folder
        Dim tFolder As Scripting.folder
        Dim tFile As Scripting.File
        Dim nFolder, sFolder, fPath, pFolder, nFile, sFile, tSingleFile As String
        Dim tEnd

        
        tSingleFile = Node1.Key
        Forms!Addpics!FilePathLbl = tSingleFile
        tClient = Me.Klantnaam
        pFolder = sPath
                        
        Set tParent = fso.GetFolder(sPath)
       
        For Each tFolder In tParent.SubFolders
                   
            nFolder = Node1.Key
            sFolder = tFolder.Name
        
        Next tFolder
       
        For Each tFile In tParent.Files
        
            nFile = Node1.Key
            sFile = tFile.Name
                    
        Next tFile
        

      ImageShow
      fPath = tSingleFile
      Call GetImagesTreeview(fPath, tClient)
      Forms!Addpics!FilePathLbl = tSingleFile
        
   

End Sub

found out that im my LoadImagePathToArray on the click of a file it was adding a "\" at the end of the file name so in the error event of the LoadImagePathToArray I added the single image function on Error.Nr 52.

Untill of cause I resolve the select Case event in the treeview.

Again many thanks for your help until now.

Mark
 
Hi MarkK,

I have managed to solve the Select Case problem edited code as follows...

Code:
Private Sub CreateNodes(Node1 As MSComctlLib.Node)

   Dim fdParent As Scripting.folder
   Dim fd As Scripting.folder
   Dim fFile As Scripting.File
   Dim Node2 As MSComctlLib.Node
   Dim k As String
   Dim fso As New Scripting.FileSystemObject
   
Set fdParent = fso.GetFolder(Node1.Key)
   
   For Each fd In fdParent.SubFolders

      k = Node1.Key & "\" & fd.Name
      Set Node2 = tvw.Nodes.Add(Node1.Key, tvwChild, k, fd.Name, 3)

      Node1.Tag = fd

      tvw.Nodes.Add k, tvwChild, "*" & k
      
   Next fd
   

   For Each fFile In fdParent.Files

      k = Node1.Key & "\" & fFile.Name

      Set Node2 = tvw.Nodes.Add(Node1.Key, tvwChild, k, fFile.Name, 4)
      
      Node1.Tag = fFile '............Added tag for file "Forgot that" :-)
      
   Next fFile
         
   If Not Node1.Child Is Nothing Then Node1.Child.EnsureVisible
   
   Exit Sub

         MsgBox Err & " " & Err.Description

   
End Sub


Code:
Private Sub tvw_NodeClick(ByVal Node1 As MSComctlLib.Node)
    Dim fso As New Scripting.FileSystemObject
    
        Dim nFolder, sFolder, fPath, pFolder, nFile, tSingleFile, tClient As String
        Dim tParent As Scripting.folder
        Dim tFolder As Scripting.folder
        Dim tFile As Scripting.File
        Dim fd, fFile
        
        tSingleFile = Node1.Key
        tClient = Me.Klantnaam
        
        
   If Node1.Selected Then '....added if node selected 
                              
      Select Case Node1.Tag
     
         Case fd ' Defined the case of fd
         
            ImageShow
            fPath = tSingleFile
            Call GetImagesTreeview(fPath, tClient)
            Forms!Addpics!FilePathLbl = tSingleFile
            
            
         Case fFile ' Defined the case of fFile......
                   
           Forms!Addpics!FilePathLbl.SetFocus
           Forms!Addpics!ImgSingle.Picture = fFile
           Forms!Addpics!Tab1.Pages(0).SetFocus
           ImageHide
        

      End Select
      
   End If

End Sub

But I still have the error that when a file is clicked, it passes the variable to the "LoadImagePathToArray" as the path + filename and then adds \ at the end of the file name....

example C:\DB_Data\Data\mark\Photos\DSC5285.jpg\

Do you know how i can remove the "\" at the end of the file before it gets passed the the "LoadImagePathToArray"?

And it is not seeing the second case "Case fFile" :banghead:

Thank you in advance.
 
Last edited:

Users who are viewing this thread

Back
Top Bottom