Results 1 to 11 of 11

Thread: Newbie Treeview Remove Nodes Question

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Feb 2003
    Posts
    146

    Newbie Treeview Remove Nodes Question

    In my treeview, I'm trying to loop through and remove all the nodes that don't have children. I found some code which seemed to work for the folks on the thread, but I can't get it to work with mine. It keeps stopping on the Remove line, saying "Invalid Key." I've found a couple of explanations with other scenarios for the "Invalid key" message but I'm too much of a newb to fix it as it relates to what I'm trying to do. Can anyone explain it in a little detail what I need to do to fix it?

    Here is the code:

    Code:
     Dim x As Node, y() As Node: ReDim y(0)
        For Each x In Tree1.Nodes
            If x.Child Is Nothing Then
                Set y(UBound(y)) = x
                ReDim Preserve y(UBound(y) + 1)
            End If
        Next
        
        Dim i As Long
        For i = 0 To UBound(y) - 1
            Tree1.Nodes.Remove y
        Next
    Thanks

    EDIT: Thanks Bruce
    Last edited by pinkclown; Jan 5th, 2004 at 01:55 PM.

  2. #2
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758
    The parameter to the Remove method is either the Index(numeric) or Key(string) of the Node. The y variable is an array of nodes. First, you need to indicate which element of the array, ie which node, you want to remove and then specify either the Index or Key of that Node. Sorry for the bad explanation but I hope you understand.

    VB Code:
    1. Dim i As Long
    2. For i = 0 To UBound(y) - 1
    3.      Tree1.Nodes.Remove y(i).key 'or y(i).Index
    4. Next

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Feb 2003
    Posts
    146
    Hmm now working on this project again I just noticed something else

    It is working great except it removes all children from every node. In other words, with the nodes that have children, it does display the node, but it also goes in and removes its children because they themselves don't have children. Is there any way to specify that it only remove a node if the "top level" doesn't have children, and then don't bother checking its children's children?

    Here's a repost of the code

    Code:
       Dim x As Node, y() As Node: ReDim y(0)
        For Each x In tvwTree.Nodes
            If x.Child Is Nothing Then
                Set y(UBound(y)) = x
                ReDim Preserve y(UBound(y) + 1)
            End If
        Next
    
    Dim i As Long
    For i = 0 To UBound(y) - 1
        
         tvwTree.Nodes.Remove y(i).Index
    Next
    Thanks in advance

  4. #4
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431
    VB Code:
    1. Dim x As Node, y() As Node: ReDim y(0)
    2.     For Each x In tvwTree.Nodes
    3.         If x.Parent.Index = 1 Then ' Only nodes that are direct children of the root qualify
    4.             If x.Child Is Nothing Then
    5.                 Set y(UBound(y)) = x
    6.                 ReDim Preserve y(UBound(y) + 1)
    7.             End If
    8.         End If
    9.     Next

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Feb 2003
    Posts
    146
    Thanks Martin, I'm sure it would work but I think I don't have my parent/child set properly. It keeps giving an "object variable or with block variable not set" on the "If x.Parent.Index = 1 Then" line.

    What the program is supposed to be doing is making a tree of multiple folders that each have shortcuts in them (the folder names are the nodes, the shortcuts are the children). This is how I have the node creation lines set up... anyone know what am I doing wrong I know it's gotta be something easy...

    Code:
    Set nParent = tvwTree.Nodes.Add(, , strFolderName, strFolderName)
    
    Set nNode = tvwTree.Nodes.Add(nParent, tvwChild, , strLinkName)
    Thanks

  6. #6
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431
    No, it's my fault.

    VB Code:
    1. For Each x In tvwDemo.Nodes
    2.         If x.Index <> 1 Then
    3.             If x.Parent.Index = 1 Then ' Only nodes that are direct children of the root qualify
    4.                 If x.Child Is Nothing Then
    5.                     Set y(UBound(y)) = x
    6.                     ReDim Preserve y(UBound(y) + 1)
    7.                 End If
    8.             End If
    9.         End If
    10.     Next

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Feb 2003
    Posts
    146
    Thanks Martin, but it's still giving the "object variable or with block variable not set" error

  8. #8

  9. #9

    Thread Starter
    Addicted Member
    Join Date
    Feb 2003
    Posts
    146
    I did.. here is the code. I left some of the other things I've tried in there, just commented.

    Code:
    Private Sub MakeNodes()
    
    Dim strFolderName As String
    Dim strShortcutLoc As String
    Dim arrFolderSplitter() As String
    Dim arrShortcutSplitter() As String
    Dim fFile As Integer
    Dim strFileName As String
    Dim intCount As Integer
    Dim intArrayCount As Long
    
    
    fFile = FreeFile()
    Set fso = New FileSystemObject
    Set fsfolder = fso.GetFolder(strAppFolderPath)
    intCount = 0
    
          
    For Each fsfoldername In fsfolder.SubFolders
        intCount = intCount + 1
        arrFolderSplitter = Split(fsfoldername, "\")
        strFolderName = arrFolderSplitter(UBound(arrFolderSplitter))
        
        If IsNumeric(strFolderName) Then
            MsgBox "oops"
        Else
        
            Dim n As Node
            
    '        Set n = tvwTree.Nodes.Add(, , , strFolderName)
            
        Set nodParent = tvwTree.Nodes.Add(, , strFolderName, strFolderName)
        
    
        End If
    
        Set fsShortcutFolder = fso.GetFolder(fsfolder & "\" & strFolderName)
             
             For Each fsfile In fsShortcutFolder.Files
                intArrayCount = intArrayCount + 1
                arrFileName = Split(fsfile, "\")
                strFileName = arrFileName(UBound(arrFileName))
                
                If Right(strFileName, 4) = ".lnk" Then
                
                    strFileName = Left(strFileName, (Len(strFileName) - 4))
                 '   tvwTree.Nodes.Add n, , strFileName, strFileName
                   tvwTree.Nodes.Add strFolderName, tvwChild, , strFileName
               '   Set nodNode = tvwTree.Nodes.Add(nodParent, tvwChild, , strFileName)
     
                    
                End If
                
                ReDim Preserve arrFileList(intArrayCount)
                arrFileList(intArrayCount) = fsfile
             Next
            
    Next
    
    
    
       Dim x As Node, y() As Node: ReDim y(0)
        For Each x In tvwTree.Nodes
    
            If x.Child Is Nothing Then
                If x.Index <> 1 Then
                    If x.Parent.Index = 1 Then
                        Set y(UBound(y)) = x
                        ReDim Preserve y(UBound(y) + 1)
                    End If
                End If
            End If
        Next
    
    Dim i As Long
    For i = 0 To UBound(y) - 1
        
         tvwTree.Nodes.Remove y(i).Index
    Next
    
    
    
    
    End Sub
    Thanks

  10. #10

    Thread Starter
    Addicted Member
    Join Date
    Feb 2003
    Posts
    146

  11. #11
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758
    A top level node does not have a Parent (all nodes do have a Root) object. Check if the Node's Parent is nothing before checking its Index.

Posting Permissions

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



Click Here to Expand Forum to Full Width