Dany
Dany

Reputation: 131

Move TReeView Nodes to a Root Node (that Contains All) Vb.net

I Have a treeView Already populated (in vb.net) and would Like to move all the nodes or all the Tree for this matter, to a Root node that Contains it all

I Have this:

Root1
---Water
---Dirt
-----Fire
-----Stone
---UFOs
Root2
---Acid
-----H20
-----TNT

And put all into one "Megaroot"

MEgaRoot
---Root1
------Water
------Dirt
--------Fire
--------Stone
------UFOs
---Root2
------Acid
--------H20
--------TNT

Maybe it is easy, but it is one of those days when i havo no idea how to approach to this.

Thank you all for the response

PROBLEM SOLVED:

Dim Counter As Integer = trvItems.Nodes.Count
Dim oldRoot As TreeNode
Dim newRoot = New TreeNode("Megaroot")
For i As Integer = 0 To Counter - 1
    oldRoot = trvItems.Nodes(0)
     trvItems.Nodes.Remove(oldRoot)
     newRoot.Nodes.Add(oldRoot)
Next i
newRoot.Expand()

Upvotes: 0

Views: 2308

Answers (1)

Hans Passant
Hans Passant

Reputation: 942538

You need to remove the old root from the tree so you can give it a new parent. Create a new root then add the old one to it. Like this:

    Dim oldRoot = TreeView1.Nodes(0)
    TreeView1.Nodes.Remove(oldRoot)
    Dim newRoot = New TreeNode("Megaroot")
    newRoot.Nodes.Add(oldRoot)
    TreeView1.Nodes.Insert(0, newRoot)
    newRoot.Expand()    '-- or ExpandAll()

Upvotes: 1

Related Questions