|
-
Mar 20th, 2003, 10:02 PM
#1
Thread Starter
The picture isn't missing
Treeview Traversing
i have a class:
VB Code:
Public Class FilesFoldersList
Inherits Windows.Forms.TreeView
Public Quit As Boolean = False
Public Function FindNode(ByVal TNC As TreeNodeCollection, ByVal Text As String) As Windows.Forms.TreeNode
Dim TN 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
FindNode(TN.Nodes, Text)
Next
End Function
End Class
which i use to find a certain node in a treeview, and this is how i use it:
VB Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim TN As TreeNode
TN = FFList.FindNode(FFList.Nodes, "C:\timem")
MsgBox(TN.Text)
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  .
-
Mar 24th, 2003, 12:00 PM
#2
Addicted Member
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
-
Mar 24th, 2003, 11:46 PM
#3
Thread Starter
The picture isn't missing
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  .
-
Mar 26th, 2003, 12:52 PM
#4
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
-
Mar 26th, 2003, 01:24 PM
#5
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:
Public Function FindNode(ByVal nds As TreeNodeCollection, ByVal text As String) As TreeNode
Dim nd As TreeNode
For Each nd In nds
'search current node
If nd.Text = text Then Return nd
'search child nodes
Dim subnode As TreeNode = FindNode(nd.Nodes, text)
'if found then return otherwise keep looking
If Not subnode Is Nothing Then Return subnode
Next
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|