• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 259
  • Last Modified:

Retrieving keys for Treeview

Can somebody provide me with the code to retrieve the parent key when clicking on a child in a Treeview.  

Thanks

0
DebeG
Asked:
DebeG
  • 4
  • 2
1 Solution
 
fds_fatboyCommented:
Private Sub TreeView1_NodeClick(ByVal Node As MSComctlLib.Node)
    If Not Node.Parent Is Nothing Then
        MsgBox Node.Parent.Key
    End If
End Sub
0
 
DebeGProgrammerAuthor Commented:
Ok, that was easy enough and I will give you credit.  But, what if I wanted to retrieve the key of a parent two levels up, or the parent of a parent.  
0
 
fds_fatboyCommented:
Private Sub TreeView1_NodeClick(ByVal Node As MSComctlLib.Node)
    If Not Node.Parent Is Nothing Then
        If Not Node.Parent.Parent Is Nothing Then
            MsgBox Node.Parent.Parent.Key
        End If
    End If
End Sub
'How many levels do you want me to go to?
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
fds_fatboyCommented:
'This is more generic, more flexible (and a lot more code)
Private Sub TreeView1_NodeClick(ByVal Node As MSComctlLib.Node)
    MsgBox GetParentNodeKey(Node,2)
End Sub

Private Function GetParentNodeKey(Node As MSComctlLib.Node, ByVal Level As Long)  As String
'Level - 1 = Parent, 2 = Grandparent, 3 = Great-grandparent etc
    Dim ParentNode As MSComctlLib.Node
    Dim LevelIndex As Long

    Set ParentNode = GetParentNode(Node, Level)
    If Parent Node Is Nothing Then
        GetParentNodeKey = vbNullString
    Else
        GetParentNodeKey = ParentNode.Key
    End If

    Set ParentNode = Nothing
End Function

Private Function GetParentNode(Node As MSComctlLib.Node, ByVal Level As Long) As MSComctlLib.Node
'Level - 1 = Parent, 2 = Grandparent, 3 = Great-grandparent etc
    Dim ParentNode As MSComctlLib.Node
    Dim LevelIndex As Long

    Set ParentNode = Node

    For LevelIndex = 1 to Level
         If ParentNode Is Nothing Then Exit For
         Set ParentNode  = ParentNode.Parent
    Next

    Set GetParentNode = ParentNode
    Set ParentNode = Nothing
End Function
0
 
DebeGProgrammerAuthor Commented:
For some reason when I type in NODE. a list doesn't appear and this is my first time working with treeviews so I didn't know what my options were.   Thanks for your assistance.
0
 
fds_fatboyCommented:
>>  ...a list doesn't appear...

This normally happens when there is something in the code causing intellisense to get confused. Check your references and declarations (Variables/Subs/Functions/properties etc) - these are the most likely causes.

If you hit ctrl+F5 to run with full compile, this will point out any problems with the code.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now