identifying clicked node

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
 
G

GB

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.
 
D

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.

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

Stefan Hoffmann

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 <--
 
S

Stefan Hoffmann

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 <--
 
K

Ken Warthen

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
 
D

Dorian

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".
 
D

Dale Fye

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top