Results 1 to 5 of 5

Thread: Treeview Traversing

  1. #1

    Thread Starter
    The picture isn't missing BuggyProgrammer's Avatar
    Join Date
    Oct 2000
    Location
    Vancouver, Canada
    Posts
    5,217

    Treeview Traversing

    i have a class:
    VB Code:
    1. Public Class FilesFoldersList
    2.     Inherits Windows.Forms.TreeView
    3.     Public Quit As Boolean = False
    4.     Public Function FindNode(ByVal TNC As TreeNodeCollection, ByVal Text As String) As Windows.Forms.TreeNode
    5.         Dim TN As TreeNode
    6.         For Each TN In TNC
    7.             If Quit = True Then Exit Function
    8.             Debug.WriteLine(TN.Text)
    9.             If TN.Text = Text Then
    10.                 FindNode = TN
    11.                 Quit = True
    12.                 Exit Function
    13.             End If
    14.             FindNode(TN.Nodes, Text)
    15.         Next
    16.  
    17.     End Function
    18. End Class

    which i use to find a certain node in a treeview, and this is how i use it:

    VB Code:
    1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    2.         Dim TN As TreeNode
    3.         TN = FFList.FindNode(FFList.Nodes, "C:\timem")
    4.         MsgBox(TN.Text)
    5.     End Sub

    the FindNodes function works, but it won't return anything. I checked everything and it should work. it keeps saying that TN=Nothing. anyone see what is the problem?
    Remember, if someone's post was not helpful, you can always rate their post negatively .

  2. #2
    Addicted Member DJ_Catboy's Avatar
    Join Date
    Jan 2003
    Location
    Suffolk, UK
    Posts
    159
    Hi,

    I think it is because you are calling the same function over and over - so the value gets lost. Remember, as you go down one "Node path" you are calling "FindNode" again, and when you find the value that you want and set the return value, it is returning that value back to itself again, and not to the first instance.

    Thats the problem - not sure of the solution! Probably a public variable. You could set a public variable with the found value instead of the boolean flag - that way each time you call the "FindNode" function it will just quit out until it gets back to the original calling program or something...?

    Hope i was helpful!
    DJ

  3. #3

    Thread Starter
    The picture isn't missing BuggyProgrammer's Avatar
    Join Date
    Oct 2000
    Location
    Vancouver, Canada
    Posts
    5,217
    i know that it is the recursin that is screwing it up, but i place breakpoints all over the place and it doesn't call it again after it finds the correct node. I changed the code to use a public variable a few days ago, but this is a bad way to do things as you have to make sure you reset the variable after you are finish with it
    Remember, if someone's post was not helpful, you can always rate their post negatively .

  4. #4
    PowerPoster 2.0 Negative0's Avatar
    Join Date
    Jun 2000
    Location
    Southeastern MI
    Posts
    4,367
    You need to store the value that is being returned from the recursive call. Otherwise it gets lost:

    Code:
            
    Dim TN As TreeNode
    Dim TMP as TreeNode
    For Each TN In TNC
                If Quit = True Then Exit Function
                Debug.WriteLine(TN.Text)
                If TN.Text = Text Then
                    FindNode = TN
                    Quit = True
                    Exit Function
                End If
                
             TMP= FindNode(TN.Nodes, Text)
             'Then check if it is nothing, if it is not nothing, then return it in FindNode
            Next

  5. #5
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    Your function never returns anything, there is no returns in it. It sets the FindNode value, which is basically the samething, but it only does it on the first pass not the recursive ones.
    Try this: (I changed it if you were already here)

    VB Code:
    1. Public Function FindNode(ByVal nds As TreeNodeCollection, ByVal text As String) As TreeNode
    2.         Dim nd As TreeNode
    3.         For Each nd In nds
    4.             'search current node
    5.             If nd.Text = text Then Return nd
    6.  
    7.             'search child nodes
    8.             Dim subnode As TreeNode = FindNode(nd.Nodes, text)
    9.             'if found then return otherwise keep looking
    10.             If Not subnode Is Nothing Then Return subnode
    11.         Next
    12.     End Function
    Last edited by Edneeis; Mar 26th, 2003 at 01:40 PM.

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