[VB.NET] Recursive search through a treeview
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. (This is the IE Web Control Treeview which is not supported by Microsoft)
VB Code:
Public Function FindAndReturnNodeIndex(ByRef tnc As TreeNodeCollection, ByVal strID As String) As String
Dim tnRec As TreeNode
Dim strTemp As String
Dim tnRet As TreeNode
For Each tnTemp As TreeNode In tnc
If tnTemp.ID = strID Then
Return tnTemp.GetNodeIndex
Exit Function
End If
Next
For Each tnRec In tnc
tnRet = SearchChildNodes(tnRec, strID)
If Not tnRet Is Nothing Then
Return tnRet.GetNodeIndex
End If
Next
End Function
Public Function SearchChildNodes(ByRef tn As TreeNode, ByVal strID As String) As TreeNode
Dim tnTemp As TreeNode
Dim tnRec As TreeNode
For Each tnTemp In tn.Nodes
If tnTemp.ID = strID Then
Return tnTemp
Else
If (tnTemp.Nodes.Count > 0) Then
tnRec = SearchChildNodes(tnTemp, strID)
If Not tnRec Is Nothing Then
Return tnRec
End If
End If
End If
Next
End Function
Call it like so:
VB Code:
Dim asdfy As String = FindAndReturnNodeIndex(Me.tvFamilyTree.Nodes, "TimeCard2")