Results 1 to 5 of 5

Thread: Tree View and Paths

  1. #1

    Thread Starter
    Frenzied Member Jotaf98's Avatar
    Join Date
    Jun 2000
    Location
    I'm not gonna give you my IP address! Ok... Portugal, South-Western Europe, 3rd rock from the sun (our star is easy to find, a 47 Ursae Majoris in the Milky Way :p )
    Posts
    1,457
    Well, this problem has been bugging me the whole day, so I though maybe you experts could help me...

    The idea is really simple: when you click cmdAdd, the contents of Text1 are added to TreeView1. It's supposed to receive a "path", delimited with "\" characters, and add nodes to the treeview according to them, much like in Windows Explorer.

    My code is as follows:

    Code:
    Private Sub cmdAdd_Click()
        On Error Resume Next
        
        Dim TempArray() As String
        Dim TempStr As String
        Dim FolderFound As Boolean
        
        TempArray = Split(Text1.Text, "\")
        
        For i = 0 To UBound(TempArray) + 1
            TempStr = TempStr & TempArray(i) & "\"
            
            For j = 1 To TreeView1.Nodes.Count
                If TreeView1.Nodes(j).FullPath & "\" & TreeView1.Nodes(j).Text = TempStr & "\" Then
                    FolderFound = True
                    Exit For
                End If
            Next j
            
            If Not FolderFound Then
                TreeView1.Nodes.Add i + 1, tvwChild, , TempArray(i)
            End If
        Next i
    End Sub
    But it doesn't work!

    I hope you can help me on this one.

    Thanks for reading!

    Bye,

    -Jotaf98

  2. #2
    Guest
    Well, what makes it "Not work"?

  3. #3
    Hyperactive Member
    Join Date
    Jun 2000
    Location
    Auckland, NZ
    Posts
    411

    A few errors:

    Hello Jotaf98,
    I think you would find the bugs easier if you did not include on Error Resume Next. In your code, you should not get ANY errors if it is written properly.

    If you examine the code below which I have tried to keep as similar as possible to your original, you may see where the flaws were in your code.

    Good Luck

    Code:
    Private Sub cmdAdd_Click()
        'On Error Resume Next
        
        Dim TempArray() As String
        Dim TempStr As String
        Dim FolderFound As Boolean
        Dim i, j As Integer
        Dim myParent As Long
        TempArray = Split(Text1.Text, "\")
            
        myParent = 0
        
        For i = 0 To UBound(TempArray)
            TempStr = TempStr & TempArray(i) & "\"
            
            For j = 1 To TreeView1.Nodes.Count
              FolderFound = False
              Debug.Print TreeView1.Nodes(j).FullPath
                If TreeView1.Nodes(j).FullPath & "\" = TempStr Then
                    FolderFound = True
                    myParent = TreeView1.Nodes(j).Index
                    Exit For
                End If
            Next j
            
            If Not FolderFound Then
              If myParent = 0 Then
                myParent = TreeView1.Nodes.Add(, tvwFirst, , TempArray(i)).Index
              Else
                myParent = TreeView1.Nodes.Add(myParent, tvwChild, , TempArray(i)).Index
              End If
            End If
            
        
        Next i
    End Sub
    Regards
    Paul Lewis

  4. #4
    Guest
    Sorry, about the last post. I got the error and here's the solution:

    Code:
    Public x As Long
    
    Private Sub Form_Load()
        TreeView1.Style = tvwTreelinesPlusMinusText
        TreeView1.LineStyle = tvwRootLines
        TreeView1.Indentation = 1
    End Sub
    
    Private Sub cmdAdd_Click()
        On Error Resume Next
        Dim TempArray() As String
        Dim TempStr As String
        Dim FolderFound As Boolean, i As Long, j As Long, k As Long
        
        TempArray = Split(Text1.Text, "\")
        
        For i = 0 To UBound(TempArray)
            TempStr = TempArray(i)
            
            For j = 1 To TreeView1.Nodes.Count
                If TreeView1.Nodes(j).Text = TempStr Then
                    FolderFound = True
                    TreeView1.SelectedItem = TreeView1.Nodes(j)
                    Exit For
                End If
            Next j
            
            If Not FolderFound Then
                x = x + 1
                If i > 0 Then TreeView1.Nodes.Add TreeView1.SelectedItem.Index, tvwChild, Str(x) & "x", TempArray(i) Else TreeView1.Nodes.Add , , Str(x) & "x", TempArray(i)
                TreeView1.SelectedItem = TreeView1.Nodes(Str(x) & "x")
            End If
            
            FolderFound = False
        Next i
    End Sub

  5. #5

    Thread Starter
    Frenzied Member Jotaf98's Avatar
    Join Date
    Jun 2000
    Location
    I'm not gonna give you my IP address! Ok... Portugal, South-Western Europe, 3rd rock from the sun (our star is easy to find, a 47 Ursae Majoris in the Milky Way :p )
    Posts
    1,457

    Talking

    Thanks everyone!

    The code worked almost ok when I first made it, but there was a small flaw that I just couldn't work out, so I started messing with the code and the result was what you all saw

    Bye

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