Lonely Wolf
November 24th, 1999, 04:55 AM
In a treeview control user can add nodes and child nodes.
The problem is: i don't know how many child nodes and respective keys there are in a node.
it's not clear, so an example:
a
|-b
||-b1
|||-b1a
|||-b2a
||-b2
||-b3
|-c
|-d
i've to delete the b node and every children, but i need to know the child's key (b1, b2, b3, b1a, b2a).
Is it possible without a recursive function?
(I know my english is'nt good)
Chris Eastwood
November 24th, 1999, 05:54 AM
You don't need to know how many children a node has if you are going to remove that node.
Removing a node from the treeview automatically deletes any child nodes associated to it.
You can use the 'node.children' property to see how many children it has, but this won't recurse through the children of it's children (if you follow me ;-) )
to see how to remove nodes, paste the following code into a form with a treeview (TreeView1) and a button (Command1)
option Explicit
private Sub Command1_Click()
Dim sKey as string
sKey = InputBox("Node Key to Remove ?", "Remove Node")
If len(sKey) = 0 then Exit Sub
on error resume next
Err.Clear
TreeView1.Nodes.Remove (sKey) ' this removes all child nodes of the node also
If Err then
MsgBox "error removing node " & sKey
End If
End Sub
private Sub Form_Load()
Dim n as Node
TreeView1.Indentation = 150
TreeView1.LineStyle = tvwRootLines
set n = TreeView1.Nodes.Add(, , "A", "A")
set n = TreeView1.Nodes.Add("A", tvwChild, "B", "B")
set n = TreeView1.Nodes.Add("B", tvwChild, "B1", "B1")
set n = TreeView1.Nodes.Add("B1", tvwChild, "B1A", "B1A")
set n = TreeView1.Nodes.Add("B", tvwChild, "B2", "B2")
set n = TreeView1.Nodes.Add("B", tvwChild, "B3", "B3")
set n = TreeView1.Nodes.Add("A", tvwChild, "C", "C")
set n = TreeView1.Nodes.Add("A", tvwChild, "D", "D")
End Sub
Chris Eastwood
CodeGuru - the website for developers
http://codeguru.developer.com/vb
Lonely Wolf
November 24th, 1999, 06:02 AM
I wasn't clear enought.
I have to know every child key because i've to delete it from a recordset (every node is a record in an acces table, the key in the table is the same in the control).
So how can i get the key of every child?
Chris Eastwood
November 24th, 1999, 06:15 AM
You'll have to use recursion (well, I'm sure there are messier ways) - it's pretty simple really,
Say you want to look at all the keys under your root node :
'
' Call this routine with :
'
' RecursiveNode TreeView1.Nodes("ROOT") ' where "ROOT" is the key
'
private Function RecursiveNode(nNode as Node) as Long
'
Dim nNodeChild as Node
Dim iIndex as Integer
'
' get Details for item
'
Debug.print nNode.Key ' here's where you get the root key
set nNodeChild = nNode.Child
'
' Now walk through the current parent node's children
'
Do While Not (nNodeChild is nothing)
'
' If the current child node has it's own children...
'
RecursiveNode nNodeChild
'
' get the current child node's next sibling
'
set nNodeChild = nNodeChild.next
Loop
End Function
Chris Eastwood
CodeGuru - the website for developers
http://codeguru.developer.com/vb