Re: Name Nodes from Array
Before you can access a treeview node's property, the node has to exist or was already added to the treeview's nodes collection. Also, how do you intend to relate the grandchild nodes? Do they all fall under one child node?
1 Attachment(s)
Re: Name Nodes from Array
The attached document show what I am trying to achieve.
At the moment I am loading it all from the form_load, by code,
vb Code:
tvwCode.Nodes.Add "One", tvwChild, "API", "API", "Folder Closed"
but I am trying to get it so I load the node names from a database, then add the nodes required.
I am tring to do it this way, so in the future the user can add a node, that new node would be stored in the database, and then, the next time the app is run, it would have the new node as well.
I hope you can understand this. :)
Re: Name Nodes from Array
The grandchild nodes fall under different childnodes.
I have tried this and now get "Element not found" EDIT This now works, or should I say goes through the code, but still doesn't show any new nodes on the treeview.
vb Code:
Private Sub Command1_Click()
Dim ctr As Integer
With TreeView1
For ctr = 0 To UBound(MyNodes)
.Nodes.Add "Main", tvwChild, "Num " & ctr, MyNodes(ctr) <------here
Next
End With
End Sub
I have also added code to set the first node before this code..
vb Code:
TreeView1.Nodes.Add , , "Main", "Numbers"
the array's elements are all loaded correctly as well.
Re: Name Nodes from Array
Storing node names is not enough. You also have to store level and parent-child relationship of the nodes.
Re: Name Nodes from Array
So, if I am storing the names of the nodes in a database, then I would have to sore this is another field, or in the same, but split the string when loaded into the array?
possibly a two dimentional array?
Re: Name Nodes from Array
What are the fields in the database, and how are your retrieving the info from the database (eg. how come you ended up with an array instead of a recordset)
Re: Name Nodes from Array
TreeView1.Nodes.Add accepts the following arguments
-relative
-relationship
-key
-text
It would be best to store those values in the database (in separate fields), so when setting up the treeview again you just plugin the stored values.
Re: Name Nodes from Array
ahh (I think)
So instead of storing just
"VB Code"
in the array, store
"Main", tvwChild, "VBCode", "VB Code"
Is this what you mean?
Re: Name Nodes from Array
Relative is an index value, not string.
When storing (and your using the child relationship and the node has a parent), relative field = node.Parent.Index
Above implies that you will have to store/process your root level nodes differently (since they have no parents). For these nodes, store relative = 0 in DB. When setting up the treeview from recordset, if rs.Fields("relative").Value = 0 then leave that argument blank ...
TreeView1.Nodes.Add , tvwLast, rs.Fields("Key").Value & "", rs.fields("NodeName").Value & "".
Else if relative > 0 then pass all values (relative, relationship, key, text) from recordset
Re: Name Nodes from Array
Thanks leinad31, I will give this a go tomorrow and let you know how I get on :D
I am sure there will be more questions to follow :lol:
Re: Name Nodes from Array
Quote:
Originally Posted by leinad31
What are the fields in the database, and how are your retrieving the info from the database (eg. how come you ended up with an array instead of a recordset)
This is an excellent question, and the answer is ......
I am only just starting out with databases, so I assumed I had to store the retrieved data frm the DB into an array to use afterwards.
I also have to learn how to set up my database as well. :rolleyes:
Re: Name Nodes from Array
Don't forget to include a primary key (use autonum) in addition to the fields your using to maintain the nodes.