Академический Документы
Профессиональный Документы
Культура Документы
Like a real world tree, the branches or nodes of a tree view use a type of relationship so that they are not
completely independent. For example, a tree view can be based on a list that has a parent item and other child
items that depend on that parent. In real world, if you cut a branch, the branches and leaves attached to it also
disappear. This scenario is also valid for a tree view.
Most of the time, a tree has only one root but a tree in an application can have more than one root.
To create the nodes of a tree view, Microsoft Visual Studio .NET provides a convenient dialog
box you can use at design time. To display it, after adding a TreeView control to a form, you
can click the ellipsis button of its Nodes field in the Properties window. This causes the
TreeNode Editor to display:
At design time and in the TreeNode Editor, to add a branch, you can click the Add Root button.
When you do this, a node with a default but incremental name is created. To edit a node's
name, first select it in the Select Node To Edit list, then, in the Label text box, change the
The branches of a tree view are stored in a property called Nodes. The Nodes property is an
object based on the TreeNodeCollection class. As its name indicates, the Nodes property
carries all of the branches of a tree view. This means that the Nodes property in fact
represents a collection. Each member of this collection is called a node and it is an object
based on the TreeNode class.
At run time, to create a new node, call the TreeNodeCollection.Add() method which is
overloaded with two versions. One of the versions of this method uses the following syntax:
This method takes as argument the string that the branch will display. This method is also the
prime candidate to create the root node. Here is an example of calling it:
Private Sub btnCreate_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnCreate.Click
tvwCountries.Nodes.Add("World")
End Sub
The other version of the TreeNodeCollection.Add() method uses the following syntax:
This method takes as argument a TreeNode object. In other words, it expects a complete or
semi-complete branch already defined elsewhere.
The TreeNode class is equipped with various constructors you can use to instantiate it. Its
default constructor allows you to create a node without primarily giving its details. Another
TreeNode constructor has the following syntax:
This constructor takes as argument the string that the node will display. Here is an example of
using it and adding its newly create node to the tree view:
End Sub
We mentioned that the primary characteristic of a node is the text it displays. The text of a
node is stored in a property of the TreeNode class and is called Text. This allows you either
to specify the string of a node or to retrieve it when needed. Here is an example of setting it:
End Sub
Just as we called the TreeNodeCollection.Add() method to create a branch, you can call it
as many times as necessary to create additional branches. Here is an example:
Private Sub btnCreate_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnCreate.Click
tvwCountries.Nodes.Add("World")
tvwCountries.Nodes.Add("Jupiter")
tvwCountries.Nodes.Add("Neptune")
tvwCountries.Nodes.Add("Uranus")
End Sub
Me.tvwStoreItems.Nodes.Add(nodDeptStore)
End Sub
At design time and in the TreeNode Editor, to create a child node for an existing item, first
select it in the Select Node To Edit list, then click the Add Child button. This causes a child
node to be created for the selected item. To edit its name, first click it and change the string in
the Label text box.
At run time, to create a child node, first get a reference to the node that will be used as its
parent. One way you can get this reference is to obtain the returned value of the first version
of the TreeNodeCollection.Add() method. As its syntax indicates, this method returns a
TreeNode object.
We have used the default constructor of the TreeNode class and the constructor that takes as
argument a string. The TreeNode class provides another constructor whose syntax is:
The first argument of this method is the string that the new node this constructor creates will
End Sub
Using the same approach, you can create as many branches and their child nodes as you wish.
Here is an example:
.............
If you create a node and add it to a branch that already contains another node, the new node
is referred to as a sibling to the existing child node.
Node Selection
Besides looking at a node, probably the primary action a user performs on a tree is to select
an item. To select a node in the tree, the user can click it. To programmatically select a node,
assign its reference to the TreeView.SelectedNode property. Here is an example:
tvwCountries.SelectedNode = nodAmerica
End Sub
After selecting a node, the tree view indicates the item selected by highlighting it. In the
To programmatically find out what item is selected in the tree, get the value of the
TreeView.SelectedNode Boolean property. If no node is selected, this property produces
null. Alternatively, you can check the value of a node's TreeNode.IsSelected Boolean
property to find out if it is currently selected.
1. Just above the line of the btnClose_Click code, declare a global TreeNode variable named
nodSelected
2. Save all
After creating a tree, to get a reference to the first child node, you can retrieve the
TreeNode.FirstNode property. You would use code as follows:
To get a reference to the last child node, retrieve the TreeNode.LastNode property.
To get a reference to the sibling above a node, if any, you can retrieve its
TreeNode.PrevNode property. To get a reference to the sibling below a node, if any, you can
retrieve its TreeNode.NextNode property.
Deleting Nodes
When a tree contains a few nodes, the user may want to delete some of them, for any reason.
To delete a node, you can call the TreeNodeCollection.Remove() method. Its syntax is:
This method expects a reference to the node you want to delete. Another solution you can use
would consist of locating the index of the node and using it. To do this, you would call the
TreeNodeCollection.RemoveAt() method. Its syntax is:
When calling this method, pass the index of the node to be deleted. If you are already at that
node and you want to remove it, you can call the TreeNode.Remove() method. Its syntax is:
One of the characteristics of a tree in the real world is that, if you cut a branch, the other
To remove all nodes of a tree view, you can call the TreeNodeCollection.Clear() method. Its
syntax is:
Hot Tracking
In order to select an item, the user must click it or navigate to it using the keyboard.
Alternative, if you want, you can cause the items to be underlined when the mouse passes
over them:
To produce this effect, you can set to true the TreeView.HotTracking Boolean property. Its
default value is false.
The presence or absence of the lines among related nodes is controlled by the
TreeView.ShowLines Boolean property. By default, this property is set to true. If this
property is set to false, the lines among the nodes would not display. Here is an example:
Private Sub btnCreate_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnCreate.Click
Dim tvwCountries As TreeView = New TreeView
tvwCountries.Location = New Point(10, 40)
tvwCountries.Width = 220
tvwCountries.Height = 290
tvwCountries.ShowLines = False
Controls.Add(tvwCountries)
tvwCountries.SelectedNode = nodAmerica
End Sub
We saw that, to select an item, the user can click it. If the user clicks another control, the
node that was selected in the tree view loses its highlighting because the control has lost
focus. When the focus moves to another control, if you want the selected node of the tree view
to preserve its highlighting, set to false the TreeView.HideSelection Boolean property. Its
default value is true.
To find out if a node is expanded, check the value of its TreeNode.IsExpanded property.
To expand other nodes of the tree view, the user can continue clicking each node that has a +
button as necessary. To programmatically expand all nodes of a tree view, call its
TreeView.ExpandAll() method. Its syntax is:
If a node is displaying a - button, it indicates that it is showing the list of its children. To hide
the list, the user can click the - button. This action is referred to as collapsing the node. To
programmatically collapse a node, call its TreeNode.Collapse() method whose syntax is:
The user can do this for each node that is expanded. To programmatically collapse all nodes of
a tree view, call its TreeView.CollapseAll() method. Its syntax is:
When creating a node, if you plan to display an icon next to it, you can use the following
constructor of the TreeNode class:
This constructor allows you to specify the text that the node will display, the index of the
picture it will use in the ImageList property, and the picture it will display when it is selected.
If you are creating a node with its children and you want to specify its pictures, use the
following constructor of the TreeNode class:
7. Click OK
8. In the form, click the tree view and, in the Properties window, set its ImageList to imageList1
9. Double-click an unoccupied area of the form and change its Load event as follows:
.....................
Dim nodBabies As TreeNode = New TreeNode("Babies", 2, 3)
.....................
End Sub