TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
 Specialized Dev Zones Research Center eBook Library .NET Java C++ Web Dev Architecture Database Security Open Source Enterprise Mobile Special Reports 10-Minute Solutions DevXtra Blogs Slideshow

Language: VB7
Expertise: Intermediate
Apr 28, 2003

### WEBINAR:On-Demand

Building the Right Environment to Support AI, Machine Learning and Deep Learning

# 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
' subtrees

Function 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 0
End Function
``````
Marco Bellinaso

 Submit a Tip Browse "XML" Tips Browse All Tips
Comment and Contribute

(Maximum characters: 1200). You have 1200 characters left.

Thanks for your registration, follow us on our social networks to keep up-to-date