# GetNodeRelationship – Evaluates the relationship between two treeview’s nodes ``' Returns a value >0 if node1 is a parent (or grandparent) of node2' Returns a value <0 if node1 is a child (or grandchild) of node2' in both cases the number reflects the number of levels between the two nodes' Returns 0 if the two Nodes are the same or if they belong to different ' subtreesFunction GetNodeRelationship(ByVal node1 As TreeNode, ByVal node2 As TreeNode) _    As Integer    If node1 Is node2 Then        ' this is the simplest case        Return 0    ElseIf node1.FullPath.Length > node2.FullPath.Length Then        ' if the two nodes belong to the same subtree,        '  then node1 is a (grand)child of node2        If node1.FullPath.IndexOf(node2.FullPath) = 0 Then            ' do the loop only if the two nodes appear to belong to the same            ' subtree check that the relationship really exists            Do Until (node1.Parent Is Nothing)                node1 = node1.Parent                GetNodeRelationship = GetNodeRelationship - 1                If node1 Is node2 Then Exit Function            Loop        End If    Else        ' if the two nodes belong to the same subtree,        '  then node1 is a (grand)parent of node2        If node2.FullPath.IndexOf(node1.FullPath) = 0 Then            ' do the loop only if the two nodes appear to belong to the same            ' subtree check that the relationship really exists            Do Until (node2.Parent Is Nothing)                node2 = node2.Parent                GetNodeRelationship = GetNodeRelationship + 1                If node1 Is node2 Then Exit Function            Loop        End If    End If    ' Nodes belong to different subtrees    Return 0End Function``

Share the Post:

Recent Articles: