Adding items to the control at runtime is a bit more involved. All the nodes belong to the control’s Nodes collection, which is made up of TreeNode objects. To access the Nodes collection, use the following expression, where TreeView1 is the control’s name and Nodes is a collection of TreeNode objects:
TreeView1.Nodes
Code language: VB.NET (vbnet)
This expression returns a collection of TreeNode objects and exposes the proper members for accessing and manipulating the individual nodes. The control’s Nodes property is the collection of all root nodes.
To access the first node, use the expression TreeView.Nodes(0) (this is the Globe node in our example). The Text property returns the node’s value, which is a string. TreeView1.Nodes(0).Text is the caption of the root node on the control. The caption of the second node on the same level is TreeView1.Nodes(1).Text, and so on.
The following statements print the strings shown highlighted below them (these strings are not part of the statements; they’re the output that the statements produce):
Debug.WriteLine(TreeView1.Nodes(0).Text)
Countries
Debug.WriteLine(TreeView1.Nodes(0).Nodes(0).Text)
United States
Debug.WriteLine(TreeView1.Nodes(0).Nodes(0).Nodes(1).Text)
New York
Code language: VB.NET (vbnet)
Let’s take a closer look at these expressions. TreeView1.Nodes(0) is the first root node, the Countries node. Under this node, there is a collection of nodes, the TreeView1.Nodes(0).Nodes collection. Each node in this collection is a country name. The first node in this collection is United States, and you can access it with the expression TreeView1.Nodes(0).Nodes(0). If you want to change the appearance of the node United States, type a period after the preceding expression to access its properties (the NodeFont property to set its font, the ForeColor property to set it color, the ImageIndex property, and so on). Likewise, this node has its own Nodes collection, which contains the states under the specific country.
Adding New Nodes
The Add method adds a new node to the Nodes collection. The Addmethod accepts as an argument a string or a TreeNode object. The simplest form of the Add method is
newNode = Nodes.Add(nodeCaption)
Code language: VB.NET (vbnet)
where nodeCaption is a string that will be displayed on the control. Another form of the Add method allows you to add a TreeNode object directly (nodeObj is a properly initialized TreeNode variable):
newNode = Nodes.Add(nodeObj)
Code language: VB.NET (vbnet)
To use this form of the method, you must first declare and initialize a TreeNode object:
Dim nodeObj As New TreeNode
nodeObj.Text = "Tree Node"
nodeObj.ForeColor = Color.BlueViolet
TreeView1.Nodes.Add(nodeObj)
Code language: VB.NET (vbnet)
The last overloaded form of the Add method allows you to specify the index in the current Nodes collection, where the node will be added:
newNode = Nodes.Add(index, nodeObj)
Code language: VB.NET (vbnet)
The nodeObj TreeNode object must be initialized as usual. To add a child node to the root node, use a statement such as the following:
TreeView1.Nodes(0).Nodes.Add("United States")
Code language: VB.NET (vbnet)
To add a state under United States, use a statement such as the following:
TreeView1.Nodes(0).Nodes(1).Nodes.Add("New York")
Code language: VB.NET (vbnet)
The expressions can get quite lengthy. The proper way to add child items to a node is to create a TreeNode variable that represents the parent node, under which the child nodes will be added. Let’s say that the CountryNode variable in the following example represents the node United States:
Dim CountryNode As TreeNode
CountryNode = TreeView1.Nodes(0).Nodes(2)
Code language: VB.NET (vbnet)
Then you can add child nodes to the ContinentNode node:
CountryNode.Nodes.Add("New York")
CountryNode.Nodes.Add("California")
Code language: VB.NET (vbnet)
To add yet another level of nodes, the city nodes, create a new variable that represents a specific state. The Add method actually returns a TreeNode object that represents the newly added node, so you can add a state and a few cities by using statements such as the following:
Dim StateNode As TreeNode
StateNode = CountryNode.Nodes.Add("New York")
StateNode.Nodes.Add("Alberny")
StateNode.Nodes.Add("Amsterdam")
StateNode.Nodes.Add("Auburn")
Code language: VB.NET (vbnet)
Then you can continue adding states under another country as follows:
StateNode = CountryNode.Nodes.Add("United Kingdom")
StateNode.Nodes.Add("London")
StateNode.Nodes.Add("Manchester")
Code language: VB.NET (vbnet)
The Nodes Collection Members
The Nodes collection exposes the usual members of a collection. The Count property returns the number of nodes in the Nodes collection. Again, this is not the total number of nodes in the control, just the number of nodes in the current Nodes collection. The expression
TreeView1.Nodes.Count
Code language: VB.NET (vbnet)
returns the number of all nodes in the first level of the control. In the case of the Countries example, it returns the value 1. The expression
TreeView1.Nodes(0).Nodes.Count
Code language: VB.NET (vbnet)
returns the number of countries in the Countries example. Again, you can simplify this expression by using an intermediate TreeNode object:
Dim Continents As TreeNode
Continents = TreeView1.Nodes(0)
Debug.WriteLine( _
"There are " & Continents.Nodes.Count.ToString & _
" continents on the control")
Code language: VB.NET (vbnet)
The Clear method removes all the child nodes from the current node. If you apply this method to the control’s root node, it will clear the control. To remove all the cities under the California node, use a statement such as the following:
TreeView1.Nodes(0).Nodes(2).Nodes(1).Nodes.Clear
Code language: VB.NET (vbnet)
This example assumes that the third node under Countries corresponds to United States, and the second node under United Sates corresponds to California.
The Item property retrieves a node specified by an index value. The expression Nodes.Item(1) is equivalent to the expression Nodes(1). Finally, the Remove method removes a node from the Nodes collection. Its syntax is
Nodes.Remove(index)
Code language: VB.NET (vbnet)
where index is the order of the node in the current Nodes collection. To remove the selected node, call the Remove method on the SelectedNode property without arguments:
TreeView1.SelectedNode.Remove
Code language: VB.NET (vbnet)
Or you can apply the Remove method to a TreeNode object that represents the node you want to remove:
Dim Node As TreeNode
Node = TreeView1.Nodes(0).Nodes(5)
Node.Remove
Code language: VB.NET (vbnet)
There are four properties that allow you to retrieve any node at the current segment of the tree: FirstNode, NextNode, PrevNode, and LastNode. Let’s say the current node is the New York node. The FirstNode property will return the first city under New York (the first node in the current segment of the tree), and LastNode will return the last city under New York. PrevNode and NextNode allow you to iterate through the nodes of the current segment: They return the next and previous nodes on the current segment of the tree (the sibling nodes, as they’re called). See the section called “Enumerating the Nodes Collection” later in this chapter for an example.
Basic Nodes Properties
There are a few properties you will find extremely handy as you program the TreeView control. The IsVisible property is a True/False value indicating whether the node to which it’s applied is visible. To bring an invisible node into view, call its EnsureVisible method:
If Not TreeView1.SelectedNode.IsVisible Then
TreeView1.EnsureVisible
End If
Code language: VB.NET (vbnet)
How can the selected node be invisible? It can, if you select it from within your code in a search operation. The IsSelected property returns True if the specified node is selected, while the IsExpanded property returns True if the specified node is expanded. You can toggle a node’s state by calling its Toggle method. You can also expand or collapse a node by calling its Expand or Collapse method, respectively. Finally, you can collapse or expand all nodes by calling the CollapseAll or ExpandAll method of the TreeView control.