Results 1 to 3 of 3

Thread: [VB.NET] Recursive search through a treeview

  1. #1

    Thread Starter
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    [VB.NET] Recursive search through a treeview

    I'm bad with recursing, but pretty good with cursing.

    Here's what I came up with to recurse through a treeview, given an ID value for a node, and to return it's NodeIndex property.

    VB Code:
    1. Public Function FindAndReturnNodeIndex(ByRef tnc As TreeNodeCollection, ByVal strID As String) As String
    2.  
    3.         Dim tnRec As TreeNode
    4.         Dim strTemp As String
    5.         Dim tnRet As TreeNode
    6.  
    7.  
    8.  
    9.  
    10.         For Each tnTemp As TreeNode In tnc
    11.             If tnTemp.ID = strID Then
    12.                 Return tnTemp.GetNodeIndex
    13.                 Exit Function
    14.             End If
    15.         Next
    16.  
    17.  
    18.  
    19.         For Each tnRec In tnc
    20.             tnRet = SearchChildNodes(tnRec, strID)
    21.             If Not tnRet Is Nothing Then
    22.                 Return tnRet.GetNodeIndex
    23.             End If
    24.  
    25.         Next
    26.  
    27.        
    28.  
    29.        
    30.  
    31.  
    32.     End Function
    33.  
    34.     Public Function SearchChildNodes(ByRef tn As TreeNode, ByVal strID As String) As TreeNode
    35.  
    36.         Dim tnTemp As TreeNode
    37.         Dim tnRec As TreeNode
    38.  
    39.  
    40.         For Each tnTemp In tn.Nodes
    41.             If tnTemp.ID = strID Then
    42.                 Return tnTemp
    43.             Else
    44.                 If (tnTemp.Nodes.Count > 0) Then
    45.                     tnRec = SearchChildNodes(tnTemp, strID)
    46.                     If Not tnRec Is Nothing Then
    47.                         Return tnRec
    48.                     End If
    49.                 End If
    50.             End If
    51.         Next
    52.  
    53.     End Function

    Call it like so:

    VB Code:
    1. Dim asdfy As String = FindAndReturnNodeIndex(Me.tvFamilyTree.Nodes, "TimeCard2")

    Although I'll be submitting it to the codebank, anyone see any scope for improvements?

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: [VB.NET] Recursive search through a treeview

    I don't use .Net quite yet (or Treeviews), so I can't be 100% sure I haven't missed something...

    Just two comments:
    shouldn't tnTemp be defined in FindAndReturnNodeIndex?
    does Return perform an Exit Function? If not, you should use the Exit throughout (else remove it from the one place it is).

    Your recursion looks fine to me, you do the important job of "escaping" the routine if there are no nodes, or a match is found.

  3. #3

    Thread Starter
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: [VB.NET] Recursive search through a treeview

    I've declared tnTemp in the line:

    VB Code:
    1. For Each tnTemp As TreeNode In tnc

    And the rest seems fine from what you say, so thanks.

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