CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Oct 2000
    Location
    NM
    Posts
    122

    Searching a Tree Control

    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



  2. #2
    Join Date
    Jan 2000
    Location
    MO, USA
    Posts
    1,506

    Re: Searching a Tree Control

    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
    John Pirkey
    MCSD (VB6)
    http://www.stlvbug.org

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

Featured