Treeview -- Dynamic Nodes

Kayot

Well-known member
Joined
Mar 6, 2007
Messages
49
Programming Experience
3-5
I'm currently writting a program to catalog my CD's to a MySQL database. I don't have a problem with the SQL part, what gets me is the nodes in treeview. I can't figure out how to made dynamic nodes to represent the file tree.

I wondered how I'd go about adding nodes so that they could go an infinate set of branches in and down, much like a file system.

So far I tryed to long way which was just one line, but it seems impossible to get it to work dynamicaly as there isn't a way to process VB code after the compile (I wish I could use a string to build the huge node.)

VB.NET:
tvDirectory.Nodes.Add([COLOR="Red"]"Root"[/COLOR]).Nodes.Add([COLOR="Red"]"Test"[/COLOR]).Parent.Nodes.Add([COLOR="Red"]"Test2"[/COLOR])

After looking through the forums, I found a topic that sort of addressed the problem but all I could get from it was this:

VB.NET:
[COLOR="Blue"]Dim[/COLOR] tmpNode [COLOR="Blue"]As New[/COLOR] TreeNode

[COLOR="Blue"]For[/COLOR] i [COLOR="Blue"]As Integer[/COLOR] = 0 To 10
    tmpNode = [COLOR="Blue"]New[/COLOR] TreeNode
    tmpNode.Text = [COLOR="Red"]"Node"[/COLOR] & i
    tvDirectory.Nodes.Add(tmpNode)
[COLOR="Blue"]Next[/COLOR]

Which is ok if I didn't need a sub branch. What I'd like to do is build nodes by using a path syntax. like putting in the following.

VB.NET:
[COLOR="Red"][root][/COLOR]
[COLOR="Red"][root][/color][B]\[/B][COLOR="Green"]dir1[/COLOR]
[COLOR="Red"][root][/color][B]\[/B][COLOR="Green"]dir1[/COLOR][B]\[/B][COLOR="Red"]dir2[/COLOR]
[COLOR="Red"][root][/color][B]\[/B][COLOR="Green"]dir1[/COLOR][B]\[/B][COLOR="Blue"]dir3[/COLOR]
[COLOR="Red"][root][/color][B]\[/B][COLOR="Green"]dir1[/COLOR][B]\[/B][COLOR="Blue"]dir3[/COLOR][B]\[/B][COLOR="DimGray"]dir4[/COLOR]
[COLOR="Red"][root][/color][B]\[/B][COLOR="Green"]dir1[/COLOR][B]\[/B][COLOR="DarkOrange"]dir5[/COLOR]
[COLOR="Red"][root][/color][B]\[/B][COLOR="SandyBrown"]dir6[/COLOR]
etc

Is there any way to make that happen? ^-^ If so, that would rock.
 
Each Treenode have a Nodes collection of its own you can address.
Run this code, see the result, try to understand it.
VB.NET:
Dim node As TreeNode = TreeView1.Nodes.Add("root1")
node.Nodes.Add("root1sub1")
node.Nodes.Add("root1sub2")
node = node.Nodes.Add("root1sub3")
node.Nodes.Add("root1sub3sub1")
node.Nodes.Add("root1sub3sub2")
There are 7 overloads of the Add method of TreenodeCollection class where you can set different properties, all return the Treenode created (except the one that takes a Treenode as parameter).
 
Ok, I understand the code, but how do I get back to the root node to start another new child? The code above builds a child and makes sub childs, but never goes back to the root node.

I can see how I might be able to make the tree, minus the the going back to root. Shame MS didn't put in a path function that would build the tree via a path instead of child to child inheritance.
 
TreeView1.Nodes is the absolute root collection, if you mean another "root" you just have to keep a reference to that node while you need it (or search it up). There is also one additional reference through the Parent property of Treenode if you need to step up one level from current node.
 
OK, is there a way to structure a node with long strings. Like for instance using the string:

VB.NET:
node.Nodes.Add("root")
node.Nodes.Add("root\dir1")
node.Nodes.Add("root\dir1\dir2")
node.Nodes.Add("root\dir3")
etc?

Or perhaps theres a class I can use that will do that? That would be grand.
 
Ok, I think I know what I want to do. I'm going to use the script approach.

I want to get it to jump into a node, make a node, jump into that node.

If there are no more nodes in that node, it will drop back one node and see if it needs to create another node. Dos equivalent:

VB.NET:
[COLOR="Blue"][Add Dir1][/COLOR]
C:\mkdir Dir1

[COLOR="Blue"][Shift into Dir1][/COLOR]
C:\cd Dir1

[COLOR="Blue"][Add Dir2][/COLOR]
C:\Dir1\mkdir Dir2

[COLOR="Blue"][Shift into Dir2][/COLOR]
C:\Dir1\cd Dir2

[COLOR="Blue"][No need to make anymore in this dir, drop back one][/COLOR]
C:\Dir1\Dir2\cd..

[COLOR="Blue"][Make Dir3][/COLOR]
C:\Dir1\mkdir Dir3

[COLOR="Blue"][Shift into Dir3][/COLOR]
C:\Dir1\cd Dir3

[COLOR="Blue"][No need to make anymore in this dir, drop back one][/COLOR]
C:\Dir1\Dir3\cd..

[COLOR="Blue"][No need to make anymore in this dir, drop back one][/COLOR]
C:\Dir1\cd..

[COLOR="Blue"][Make Dir4][/COLOR]
c:\mkdir Dir4

[COLOR="Blue"][Shift into Dir4][/COLOR]
C:\cd Dir4

[COLOR="Blue"][No need to make anymore in this dir, drop back one][/COLOR]
C:\Dir4\cd..

[COLOR="Blue"][All Done... Dirtree Finished][/COLOR]

I'd like to make a script like this, do the above:

Add=Dir1 (I want to to automove into Dir1, etc)
Add=Dir2
Back (Back just pulls it back one node.)
Add=Dir3
Back
Back
Add=Dir4
Back

Note: The String will look like this "Add=Dir1,Add=Dir2,Back,Add=Dir3,Back,Back,Add=Dir4,Back" Though the last Back isn't needed as the whole thing is finished.

The End result Should look like the following

VB.NET:
[Root]
 ├ Dir1
 │ ├Dir2
 │ └Dir3
 └ Dir4

So all I need is two subs, one that makes a node and shifts in, and another that pulls back one node and prepare for adding/pulling back. ^-^ I have no clue how to make them either, I think a sub that controls the whole thing would be good too. I figure I'll use a single line with delimiter "," and have it split into an array so the whole command process can be looped

VB.NET:
[COLOR="Blue"]For[/COLOR] i [COLOR="Blue"]as Byte[/COLOR] = 0 [COLOR="Blue"]to[/COLOR] ArrayName.Count - 1
    [COLOR="Blue"]If[/COLOR] ArrayName(i) = [COLOR="DarkRed"]"Back"[/COLOR] [COLOR="Blue"]Then[/COLOR]
        Back()
    [COLOR="Blue"]Else[/COLOR]
        [COLOR="Blue"]Dim[/COLOR] Split() [COLOR="Blue"]as string[/COLOR] = ArrayName.Split([COLOR="DarkRed"]"="[/COLOR])
        Add(Split(1))
    [COLOR="Blue"]EndIf[/COLOR]
[COLOR="Blue"]Next[/COLOR]

So could someone help me with those subs?
 
Recursion (method calls itself) is also commonly used for processing hierarchic data. Here's an example; it's a childs play! ;)
VB.NET:
Sub addchilds(ByVal parentnode As TreeNode, ByVal parentpath As String)
    For Each childdir As String In IO.Directory.GetDirectories(parentpath)
        Dim newchild As TreeNode = parentnode.Nodes.Add(IO.Path.GetDirectoryName(childdir))
        addchilds(newchild, childdir)
    Next
End Sub
 
I'm getting thrown off by the:

VB.NET:
IO.Directory.GetDirectories(parentpath)

^-^ I'm guessing it was the Dos example. I don't want to touch the file system (For now), I'm talking about making a TreeView from an SQL entry that is a list of commands.

For instance, the string:
VB.NET:
[COLOR="DarkRed"]"Add=Dir1,Add=Dir2,Back,Add=Dir3,Back,Back,Add=Dir 4,Back"[/COLOR]

Would Array to:
VB.NET:
ArrayName(0)=[COLOR="DarkRed"]"Add=Dir1"[/COLOR]
ArrayName(1)=[COLOR="DarkRed"]"Add=Dir2"[/COLOR]
ArrayName(2)=[COLOR="DarkRed"]"Back"[/COLOR]
ArrayName(3)=[COLOR="DarkRed"]"Add=Dir3"[/COLOR]
ArrayName(4)=[COLOR="DarkRed"]"Back"[/COLOR]
ArrayName(5)=[COLOR="DarkRed"]"Back"[/COLOR]
ArrayName(6)=[COLOR="DarkRed"]"Add=Dir 4"[/COLOR]
ArrayName(7)=[COLOR="DarkRed"]"Back"[/COLOR]

And then the loop from earlier would build the TreeView with the "script". This would make it easier to make a treeview as only two commands are present and the script is already in order since it was made so it would be and was stored in the SQL database as a TEXT field.

What I want to know is how to get it to Back and how to get it to add and move into what it added while rememebering. This script is only used once when loading that entry and isn't tampered with after ward (This is a CD catalog :D)

Sorry if I miss led you.

I figured it would be something like:

VB.NET:
[COLOR="Blue"]Private Sub[/COLOR] AddNode([COLOR="Blue"]ByVal[/COLOR] Value [COLOR="Blue"]as String[/COLOR])
    [COLOR="Green"]'Adds the node to the currently selected node (Code selected, not user as this is part of a loop.
    'Besides it'd be pointless to have the user do it as it would defeat the purpose of a catalog), 
    'then goes into the new node.[/COLOR]
[COLOR="Blue"]End Sub[/COLOR]

[COLOR="Blue"]Private Sub[/COLOR] Back()
    [COLOR="Green"]'Moves a parent/child node back (Not up, but back a branch with root being all the way back meaning 
    'it can't go back any further from root. And then setting this back node as the point where the next new node can go. 
    'This leads to AddNode etc.[/COLOR]
[COLOR="Blue"]End Sub[/COLOR]

Thats whats confusing me, I have no clue how to do those subs. I've tried several approaches and none of them work.
 
Back
Top