Lonely Wolf
November 23rd, 1999, 04:32 AM
I've coded a treeview search like this:
dim i as integer
for i= 1 to treeview.nodes.count
if treeview.nodes(i).key = criteria then
...
...
exit sub
end if
next i
i= msgbox("Not find", .....)
Is it good?
Thanks
James Kline
November 23rd, 1999, 08:53 AM
The following code will convert the tv nodes to a string and you can then use instr() to find what you are looking for.
' convert the contents of a TreeView into a string
' if a Node is provided, it only parse a sub tree
' if last argument is false or omitted, all items are parsed
Function TreeViewToString(TV as TreeView, optional StartNode as Node, optional OnlyVisible as Boolean) as string
Dim nd as Node, childND as Node
Dim res as string, i as Long
static Level as Integer
' exit if there are no nodes
If TV.Nodes.count = 0 then Exit Function
If StartNode is nothing then
' if StartNode is omitted, start from the first root node
set nd = TV.Nodes(1).Root.FirstSibling
else
set nd = StartNode
End If
' output the starting node
res = string$(Level, vbTab) & nd.Text & vbCrLf
' then call recursively this routine to output all child nodes
' if OnlyVisible=Tree, do this only if this node is expanded
If nd.Children And (nd.Expanded Or OnlyVisible = false) then
Level = Level + 1
set childND = nd.Child
for i = 1 to nd.Children
res = res & TreeViewToString(TV, childND, OnlyVisible)
set childND = childND.next
next
Level = Level - 1
End If
' if we are parsing the whole tree, we must account for multiple roots
If StartNode is nothing then
set nd = nd.next
Do Until nd is nothing
res = res & TreeViewToString(TV, nd, OnlyVisible)
set nd = nd.next
Loop
End If
TreeViewToString = res
End Function
James Kline
Simonton Windows, Inc.
Chris Eastwood
November 23rd, 1999, 08:56 AM
You can use the 'key' of a node in the treeview to search for a specific node - you don't have to loop through each one to find it.
eg.
'
' find node with key of 'chris'
'
Dim n as Node
on error resume next
set n = TreeView1.Nodes("chris")
Err.Clear
on error Goto yourpropererrorhandler
If n is nothing then
MsgBox "Not Found!"
else
MsgBox "Found!"
End If
'
' You now have a reference 'n' to the node you were looking for
' - so you can reference it with : (eg)
'
' n.Expanded = true
'
' You could also reference it :
'
' TreeView1.Nodes("chris").Expanded = true
'
' etc.
'
Chris Eastwood
CodeGuru - the website for developers
http://codeguru.developer.com/vb