At oprette root er ikke noget problem, der bruger man jo bare en TreeView2().Nodes().Add("root"). Men hvordan laver jeg de underlænge mapper, som vist ovenover?
'Method 1: straightforward adding of nodes With Me.TreeView1.Nodes 'add text .Add("AddByText") 'since with..end with is used: read TreeView1.Nodes.Add ....
'every add method returns the newly created node. You can use 'this concept set the result to a variable or to directly add 'a childnode: .Add("AddByText2").Nodes.Add("ChildOfAddByText")
'this, you can take as far as you want .Add("AddByText3").Nodes.Add("ChildOfAddByText").Nodes.Add("Another child")
'-- N = .Add("AddByText, Attach To Variable") N.Nodes.Add("Child one") N.Nodes.Add("Child two") ' -- With .Add("AddByText Use WithTo Add ChildNodes").Nodes .Add("Child 1") .Add("Child 2") .Add("Child 3").Nodes.Add("Subchild 1") End With End With
'for clarity, from here on, the treeview1 name will be added. 'In everyday use, you'll probably find the use of with..end with 'a lot easier (I know I do..)
'Method 2: adding by node 'Like virtually every .Net method you can directly assign an object: Me.TreeView1.Nodes.Add(New TreeNode("AddByNode"))
'check out the overloading possibilities of using New() 'Another advantage of this method is that you can add a complete branch. '(N is already declared as TreeNode above) N = New TreeNode("MainNodeToAdd") N.Nodes.Add("Child 1") N.Nodes.Add("Child 2")
'you can for instance add this newly created node to all main branches: Dim enumNode As TreeNode For Each enumNode In TreeView1.Nodes enumNode.Nodes.Add(N.Clone) '<- the clone() method is needed Next
'Adding will always add the the node at the end of the collection. 'Of course you can also insert at a specified location: Me.TreeView1.Nodes.Insert(2, New TreeNode("I am inserted at the 3th position"))
'removing is done much in the same way: N = TreeView1.Nodes.Add("I need to be removed").Nodes.Add("and all children too") TreeView1.Nodes.Remove(N)
'to clear all branches of any node you can use clear() N.Nodes.Add("This child you will not see") N.Nodes.Clear()
'if you use Clear on the treeview nodes itself, you ' would once again have an empty treeview
'once an item has been added, it is part of the item collection in nodes 'this means you can access it by its index TreeView1.Nodes(0).Text = "I have index 0"
'the behaviour of the treenode can be controlled completely in code. 'you can make it expand TreeView1.Nodes(0).Expand()
Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.