Results 1 to 6 of 6
  1. #1
    5 Star Lounger
    Join Date
    Jul 2001
    Location
    Terneuzen, Netherlands
    Posts
    895
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Treeview & nodes (VB6)

    I'm using some code template that fills a treeview with a file & directory structure.

    trv.Nodes.Add parent_node, tvwChild, , Filename

    Some entries I want in red, so I added a global counter and this line (for the required red entries):

    trv.Nodes.Item(Cnt).ForeColor = vbRed

    When I run my code, almost all entries at the top are red, from a certain line, nothing is red. I must be confusing the Item-counter and the sub-nodes maybe...

    Is this sufficient info for someone to help me out or should I provide more code (or even a running example?)

    Thanks,

    Erik Jan

  2. #2
    Silver Lounger
    Join Date
    Mar 2001
    Location
    Springfield, Ohio, USA
    Posts
    2,136
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Treeview & nodes (VB6)

    I have a Tree View that I use for a TOC for map layers and I highlight in red items of interest, so I probably can help you, but the code is a work (where I'm headed). Offhand, I don't see anything wrong with your code, so if you could post a longer example, that would help. Just post the code, I'll have to put the control on a VBA userform. --Sam
    <font face="Comic Sans MS">Sam Barrett, CACI </font face=comic>
    <small>And the things that you have heard... commit these to faithful men who will be able to teach others also. 2 Timothy 2:2</small>

  3. #3
    5 Star Lounger
    Join Date
    Jul 2001
    Location
    Terneuzen, Netherlands
    Posts
    895
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Treeview & nodes (VB6)

    Sam,

    Thanks for your offer to help. I got the code off the internet and started to tweak this for my purposes. The -ultimate- goal is to process a directory tree with files and delete files (and directories) older and a specific number of days.

    I cleared all irrelevant things back out and am now left with just a tree display which walks the tree and (only) marks files older than a specific #days as red.

    Obviously, you'll have to add the treeview and re-define the dir-tree starting point below.

    Looking forward to your help and/or better / more / other suggestions to do this.

    Erik Jan



    Option Explicit
    Dim lngDaysOld As Long
    Dim Cnt As Long

    Private Sub Form_Load()
    Dim strCommand As String, strArray As Variant, strFileSpec As String
    '
    Cnt = 0
    lngDaysOld = 10
    strFileSpec = "d:ej_temp"
    '
    Dim fso As FileSystemObject
    Dim target_folder As Folder
    Dim target_node As Node
    Dim txt As String
    '
    trvResults.Visible = False
    trvResults.Nodes.Clear ' Clear the TreeView
    ' Get the starting folder
    Set fso = New FileSystemObject
    Set target_folder = fso.GetFolder(strFileSpec)
    ' Add the starting folder to the TreeView
    txt = target_folder.ParentFolder & "" & target_folder.Name & _
    " (" & target_folder.DateCreated & ")"
    Set target_node = trvResults.Nodes.Add(, , , txt)
    ' Search
    ListFileInfo trvResults, target_node, target_folder
    trvResults.Visible = True
    End Sub

    ' List the information about this directory under the TreeView node
    Private Sub ListFileInfo(ByVal trv As TreeView, ByVal parent_node As Node, ByVal parent_folder As Folder)
    Dim txt As String
    Dim child_folder As Folder
    Dim child_file As File
    Dim child_node As Node
    Dim dCompDate As Date
    dCompDate = Now
    ' Search subdirectories
    For Each child_folder In parent_folder.SubFolders
    txt = child_folder.Name & " (" & child_folder.DateCreated & ")"
    Set child_node = trv.Nodes.Add(parent_node, tvwChild, , txt)
    ListFileInfo trv, child_node, child_folder
    Next
    ' List the files
    For Each child_file In parent_folder.Files
    txt = child_file.Name & " (" & child_file.DateCreated & ")"
    Cnt = Cnt + 1
    trv.Nodes.Add parent_node, tvwChild, , txt
    'See if we should flag this file because it is too old
    If DateDiff("d", FileDateTime(child_file), dCompDate) >= lngDaysOld Then
    trv.Nodes.Item(Cnt).ForeColor = vbRed
    End If
    Next
    End Sub

  4. #4
    WS Lounge VIP rory's Avatar
    Join Date
    Dec 2000
    Location
    Burwash, East Sussex, United Kingdom
    Posts
    6,280
    Thanks
    3
    Thanked 191 Times in 177 Posts

    Re: Treeview & nodes (VB6)

    Hi,
    In your ListFileInfo routine, you could try replacing this:
    <pre>trv.Nodes.Add parent_node, tvwChild, , txt
    'See if we should flag this file because it is too old
    If DateDiff("d", FileDateTime(child_file), dCompDate) >= lngDaysOld Then
    trv.Nodes.Item(Cnt).ForeColor = vbRed
    </pre>

    with this:
    <pre>set child_node2 = trv.Nodes.Add(parent_node, tvwChild, , txt)
    'See if we should flag this file because it is too old
    If DateDiff("d", FileDateTime(child_file), dCompDate) >= lngDaysOld Then
    child_node2.ForeColor = vbRed
    </pre>

    Note: You will need to declare child_node2 as a Node at the top of the routine. That way you will definitely be referring to the node you have just created.
    Regards,
    Rory

    Microsoft MVP - Excel

  5. #5
    Silver Lounger
    Join Date
    Mar 2001
    Location
    Springfield, Ohio, USA
    Posts
    2,136
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Treeview & nodes (VB6)

    Very nice example! I've always wanted an example like this. I think that you only need to get rid of the counter (the reason the counter didn't work is because you didn't update it on every Add) and use a with block:
    <pre> With trv.Nodes.Add(parent_node, tvwChild, , txt)
    If DateDiff("d", FileDateTime(child_file), dCompDate) >= glngDaysOld Then _
    .ForeColor = vbRed
    End With
    </pre>


    I think that the rest of my changes were just style (although the DateCreated method always gave the current date/time). I have attached a spreadsheet with a working example, just change the directory to a valid one. I created a module with a macro to load the form and get the root folder and days from the spreadsheet:
    <pre>Option Explicit
    Public gstrFileSpec As String
    Public glngDaysOld As Long

    Public Sub showForm()
    gstrFileSpec = [nDirectory]
    glngDaysOld = [nDays]
    frmTree.Show
    End Sub
    </pre>

    Then here is your code, fixed and beautified:
    <pre>Option Explicit

    Private Sub UserForm_Initialize()
    'Private Sub Form_Load()
    Dim fso As New Scripting.FileSystemObject
    Dim target_folder As Scripting.Folder
    Dim target_node As MSComctlLib.Node
    Dim txt As String
    '
    With trvResults
    .Visible = False
    .LineStyle = tvwRootLines
    .Style = tvwTreelinesPlusMinusText
    .Nodes.Clear ' Clear the TreeView
    ' Get the starting folder
    Set target_folder = fso.GetFolder(gstrFileSpec)
    ' Add the starting folder to the TreeView
    txt = target_folder.ParentFolder & "" & target_folder.Name & _
    " (" & target_folder.DateCreated & ")"
    Set target_node = .Nodes.Add(, , , txt)
    ' Search
    ListFileInfo trvResults, target_node, target_folder
    target_node.Expanded = True
    .Visible = True
    End With
    End Sub
    '
    ' List the information about this directory under the TreeView node
    Private Sub ListFileInfo(trv As MSComctlLib.TreeView, _
    parent_node As MSComctlLib.Node, _
    parent_folder As Scripting.Folder)
    Dim txt As String
    Dim child_folder As Scripting.Folder
    Dim child_file As Scripting.File
    Dim child_node As MSComctlLib.Node
    Dim dCompDate As Date
    dCompDate = Now
    ' Search subdirectories
    For Each child_folder In parent_folder.SubFolders
    txt = child_folder.Name & " (" & FileDateTime(child_folder) & ")"
    Set child_node = trv.Nodes.Add(parent_node, tvwChild, , txt)
    ListFileInfo trv, child_node, child_folder
    Next
    ' List the files
    For Each child_file In parent_folder.Files
    txt = child_file.Name & " (" & FileDateTime(child_file) & ")"
    With trv.Nodes.Add(parent_node, tvwChild, , txt)
    'See if we should flag this file because it is too old
    If DateDiff("d", FileDateTime(child_file), dCompDate) >= glngDaysOld Then _
    .ForeColor = vbRed
    End With
    Next
    End Sub
    </pre>

    <font face="Comic Sans MS">Sam Barrett, CACI </font face=comic>
    <small>And the things that you have heard... commit these to faithful men who will be able to teach others also. 2 Timothy 2:2</small>

  6. #6
    5 Star Lounger
    Join Date
    Jul 2001
    Location
    Terneuzen, Netherlands
    Posts
    895
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Treeview & nodes (VB6)

    SammyB & Rory,

    Thanks for your tips; I've got things running and will now be able to complete my little tool!

    Thanks again!

    Erik Jan

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •