identifying clicked node

  • Thread starter Thread starter Ken Warthen
  • Start date Start date
K

Ken Warthen

I'm using a treeview control on a form in an Access 2007 database. There are
six levels in the treeview. How can I determine the level of any node
clicked by a user?

Ken
 
As stated elsewhere, although I'm not specifically familiar with how the
internals of treeview procedures work, I have worked with data trees. One
way that I can think of is within the procedure that evaluates when selecting
a node, to look at the parent object recursively until at the top of the
tree. This assumes of course that one, each node has at least one parent,
two that the root of the tree can be consistently identified by some piece of
data, even if it is that the parent is null or something similar, and three
that the relationship(s) are not circular allowing the parent of a child to
be a child of the same child without addressing that circumstance..

Otherwise you would probably have to go through the entire list of nodes to
identify which one is active/selected. :\ Not a quick task.
 
Here is what I did:

Me!KW = N.FullPath 'Nodes separated by back slashes
intLev = CountCh(Me!KW, "\") + 1 'Find level in tree

CountCh is a function that counts the "\" characters.

-- Dorian
"Give someone a fish and they eat for a day; teach someone to fish and they
eat for a lifetime".
 
hi Ken,

Ken said:
I'm using a treeview control on a form in an Access 2007 database. There are
six levels in the treeview. How can I determine the level of any node
clicked by a user?
This should work (level +-1 :):

Option Compare Database
Option Explicit

Private WithEvents m_TreeView As MSComctlLib.TreeView

Private Sub Form_Close()

Set m_TreeView = Nothing

End Sub

Private Sub Form_Load()

Set m_TreeView = TreeView0
'you need to fill it...

End Sub

Private Sub m_TreeView_DblClick()

If Not m_TreeView.SelectedItem Is Nothing Then
MsgBox "Level = " & GetNodeLevel(m_TreeView.SelectedItem)
End If

End Sub

Private Function GetNodeLevel(ANode As MSComctlLib.Node) As Long

Dim Node As MSComctlLib.Node

Dim Level As Long

Level = 0
If Not ANode Is Nothing Then
Level = 1
Set Node = ANode
Do While Not Node.Parent Is Nothing
Level = Level + 1
Set Node = Node.AParent
Loop
End If

GetNodeLevel = Level

End Function


mfG
--> stefan <--
 
hi Dorian,
Here is what I did:

Me!KW = N.FullPath 'Nodes separated by back slashes
intLev = CountCh(Me!KW, "\") + 1 'Find level in tree

CountCh is a function that counts the "\" characters.
Basically:

IntLev = Len(N.FullPath) - Len(Replace(N.FullPath, "\", "")) + 1

But it ignores the fact that the node text itself my contain backslashes
and thus report a wrong result.


mfG
--> stefan <--
 
Stefan,

Your suggestion is greatly appreciated. I did find an alternative method by
using a meaningful key code in the form load event where the treeview control
is loaded. I used L1 as the first two characters of the key for the first
level, L2 as the first two characters of the key for the second level, etc.
Then in node_click event of the treeview control I use a select case event on
left(node.key, 2) to identify each level.

Ken
 
In my case, that's not possible as all nodes are alphanumeric.
But you are right, I should have identified the possibility.
-- Dorian
"Give someone a fish and they eat for a day; teach someone to fish and they
eat for a lifetime".
 
I was going to mention that I generally set the nodes Tag value to the level
of the node as I build the tree. But this method would work just as well.
 
Back
Top