Solved

How to Quickly Find a TreeNode

Posted on 2006-11-01
1
297 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
[X]
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
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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

719 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