Solved

How to Quickly Find a TreeNode

Posted on 2006-11-01
1
296 Views
Last Modified: 2010-04-23
Hi I currently search my tree by using a recursive function.

    Public Function FindANodeRecursive(ByVal nodeList As TreeNodeCollection, ByVal myId As Object) As TreeNode
        Dim targetNode As TreeNode = Nothing
        For Each aNode As TreeNode In nodeList

            'searches for Node with matching Id.  the ID is stored in treenode.tag
            If aNode.Tag = myId Then

                Return aNode
                Exit Function

            ElseIf aNode.Nodes.Count > 0 Then

                'if nodes is a folder and has children scan it
                targetNode = Me.FindANodeRecursive(aNode.Nodes, myId)
                If Not targetNode Is Nothing Then
                    Return targetNode
                End If

            End If
        Next

        Return targetNode

    End Function

Something the recursive function is very slow.  Is there a better way of finding a treenode??

 One way I was thinking of was using a hashtable with treenode's tag as the key, and the treenode as the value.  This way I can just search the hashtable to find the right node.    Would this be a good idea? also Is there a treenode added event that can listen to when A treenode is added to my tree??  This way I don't have to go through all my code and adding the new keys each time I add a node.

0
Comment
Question by:OFGemini
1 Comment
 
LVL 10

Accepted Solution

by:
gangwisch earned 250 total points
ID: 17854424
Dim a As New TreeView
        a.Nodes.Find("nameoftreenode", True)
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question