Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

How to Quickly Find a TreeNode

Posted on 2006-11-01
1
295 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: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone 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

Suggested Solutions

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

860 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