View Full Version : Solved: Treeview nodes

06-24-2008, 02:23 PM
Is there a function available to determine which node (by index) has been selected from a tree view

this works to count the number of nodes in the tre
count = TreeView1.Nodes.count
MsgBox count & " Nodes"

When I click a node, a routine runs and updates the treeview (adding more rows/nodes) I would like the node that was selected in the first instance to be the selected (highlighted) node when the treeview rebuilds. (or even the new node added highlighted)

Is this possible?

Thnx for looking

Jan Karel Pieterse
06-24-2008, 09:48 PM
The click event of the treeview returns the clicked node as its argument:

Private Sub TreeView1_NodeClick(ByVal Node As MSComctlLib.Node)
Msgbox Node.Index
End Sub

I'd store the key and the index so you can double check if you're selecting the right node.

06-25-2008, 12:22 AM
Thanks Jan

That should help
Now I know which node was selected and how many nodes there are in the tree

After I have added a new item to the tree and I now know its index how would I make that item have focus (highlighted)


with treeview1
.selecteditem = index(12) :think:
end with

Interestingly if a new item is inserted between say nodes 12 and 13 of a 20 item tree its index is the total count +1 e.g. 21 where I would have thought it would have become item 13 and all susequent nodes increase their index by 1

Jan Karel Pieterse
06-25-2008, 02:56 AM
I'd say (untested):

with treeview1
.selecteditem = 12
end with

06-25-2008, 03:48 AM
I think I have tried every combination of


but none of them seem to work

Any other ideas?

06-25-2008, 11:26 PM

06-29-2008, 11:15 PM
Still looking for a solution to this if anybody can help.

06-30-2008, 04:06 AM
So pleased with myself
FOund how to do it (by hit and miss mainly but got there in the end :yes )

This higlights the parent node and the last node added and makes sure if the tree is larger than the frame it is displayed in that the node is visible

With Me.TreeView1
idx = .Nodes.Count
.Nodes.item(sel).BackColor = &HC0E0FF 'Node that was first selected
.Nodes.item(idx).BackColor = &HC0FFC0 'Last node added
End With