Click to See Complete Forum and Search --> : Searching a Tree Control


Codejoy
May 7th, 2001, 12:38 PM
Hello, I was curious if it is possible to search a tree control to find if a string in it exsists before adding another of the same to it using

set nodX = 1.Nodes.Add(1, tvwChild, , myRec!name)




Thanks for your help
Shane

Johnny101
May 7th, 2001, 02:34 PM
you have to loop through the tree's nodes, recursively. like this:

Function RecursiveSearchString(ParentNode as Node, sSearchFor as string) as Boolean
Dim ChildNode as Node
Dim icounter as Integer
Dim i as Integer
Dim bFoundMatch as Boolean

If ParentNode.Children then

for icounter = 1 to ParentNode.Children

set ChildNode = ParentNode.Child

'move to the second or third or fourth child under the parent
for i = 1 to icounter - 1
set ChildNode = ChildNode.next
next

If ChildNode is nothing then
Exit for
End If

If ChildNode.Children then
If RecursiveSearchString(ChildNode, sSearchFor) then
bFoundMatch = true
DoEvents
Exit for
End If

End If

If UCase(ChildNode.Text) = UCase(sSearchFor) then
bFoundMatch = true
End If
next

'we've gone through all the children, now check the parent
If UCase(ParentNode.Text) = UCase(sSearchFor) then
'we found a match - let everyone know
bFoundMatch = true
End If
else
'this node has no children, but we need to check it too
If UCase(ParentNode.Text) = UCase(sSearchFor) then
'we found a match - let everyone know
bFoundMatch = true
End If
End If

RecursiveSearchString = bFoundMatch

End Function




pass in, either the selected node to search a branch, or the root node (TreeView1.Nodes(1)) and something to search for. It isn't case sensitive right now, but you could easily make it so, by removing the UCase statements.

hope this helps,

john

John Pirkey
MCSD
http://www.ShallowWaterSystems.com
http://www.stlvbug.org