CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Nov 1999
    Location
    Italy
    Posts
    80

    Treeview search - what about this?

    I've coded a treeview search like this:

    dim i as integer

    for i= 1 to treeview.nodes.count
    if treeview.nodes(i).key = criteria then
    ...
    ...
    exit sub
    end if
    next i
    i= msgbox("Not find", .....)




    Is it good?

    Thanks

    Something over there is coding....
    ... and you don't know!

  2. #2
    Join Date
    Nov 1999
    Location
    Parkersburg, WV
    Posts
    3

    Re: Treeview search - what about this?

    The following code will convert the tv nodes to a string and you can then use instr() to find what you are looking for.


    ' convert the contents of a TreeView into a string
    ' if a Node is provided, it only parse a sub tree
    ' if last argument is false or omitted, all items are parsed

    Function TreeViewToString(TV as TreeView, optional StartNode as Node, optional OnlyVisible as Boolean) as string
    Dim nd as Node, childND as Node
    Dim res as string, i as Long
    static Level as Integer

    ' exit if there are no nodes
    If TV.Nodes.count = 0 then Exit Function
    If StartNode is nothing then
    ' if StartNode is omitted, start from the first root node
    set nd = TV.Nodes(1).Root.FirstSibling
    else
    set nd = StartNode
    End If
    ' output the starting node
    res = string$(Level, vbTab) & nd.Text & vbCrLf

    ' then call recursively this routine to output all child nodes
    ' if OnlyVisible=Tree, do this only if this node is expanded
    If nd.Children And (nd.Expanded Or OnlyVisible = false) then
    Level = Level + 1
    set childND = nd.Child
    for i = 1 to nd.Children
    res = res & TreeViewToString(TV, childND, OnlyVisible)
    set childND = childND.next
    next
    Level = Level - 1
    End If

    ' if we are parsing the whole tree, we must account for multiple roots
    If StartNode is nothing then
    set nd = nd.next
    Do Until nd is nothing
    res = res & TreeViewToString(TV, nd, OnlyVisible)
    set nd = nd.next
    Loop
    End If

    TreeViewToString = res
    End Function







    James Kline
    Simonton Windows, Inc.

  3. #3
    Join Date
    May 1999
    Location
    Oxford UK
    Posts
    1,459

    Re: Treeview search - what about this?

    You can use the 'key' of a node in the treeview to search for a specific node - you don't have to loop through each one to find it.

    eg.


    '
    ' find node with key of 'chris'
    '

    Dim n as Node

    on error resume next

    set n = TreeView1.Nodes("chris")
    Err.Clear
    on error Goto yourpropererrorhandler

    If n is nothing then
    MsgBox "Not Found!"
    else
    MsgBox "Found!"
    End If
    '
    ' You now have a reference 'n' to the node you were looking for
    ' - so you can reference it with : (eg)
    '
    ' n.Expanded = true
    '
    ' You could also reference it :
    '
    ' TreeView1.Nodes("chris").Expanded = true
    '
    ' etc.
    '





    Chris Eastwood

    CodeGuru - the website for developers
    http://codeguru.developer.com/vb

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