How to Quickly Find a TreeNode
Posted on 2006-11-01
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
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
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.