TreeView
Microsoft .NET Framework, ASP.NET, Visual C# (CSharp, C Sharp, C-Sharp) Developer Training, Visual Studio
| Exam 70-526 Preparation Guide: Configure the layout and functionality of a Windows Form to display a list of items |
Contents |
A TreeView control allows you to display a hierarchy of nodes to users, like the way files and folders are displayed in the left pane of the Windows Explorer.
Each node in a Treeview is an instance of the TreeNode class. A TreeNode contains information about its location within a TreeView and any child nodes that it may contain.
Inheritance hierarchy
System.Object
System.MarshalByRefObject
System.ComponentModel.Component
System.Windows.Forms.Control
System.Windows.Forms.TreeView
Useful properties
-
CheckBoxes- Indicates whether check boxes are displayed next to the tree nodes in the tree view control.
-
-
HideSelection- Indicates whether the selected item in the control remains highlighted when the control loses focus.
-
-
HotTracking- Indicates whether the text of an item or sub-item has the appearance of a hyperlink when the mouse pointer passes over it.
-
-
ImageIndex- Gets or sets the image-list index value of the default image that is displayed by the tree nodes.
-
-
ImageKey- Gets or sets the key of the default image for each node in theTreeViewcontrol when it is in an unselected state.
-
-
ImageList- Gets or sets the ImageList that contains theImageobjects used by the tree nodes.
-
-
Indent- Gets or sets the distance to indent each of the child tree node levels.
-
-
ItemHeight- Gets or sets the height of each tree node in the tree view control.
-
-
LineColor- Gets or sets the color of the lines connecting the nodes of the TreeView control.
-
-
Nodes- Gets the collection of tree nodes that are assigned to the tree view control.
-
-
SelectedNode- Indicates the tree node that is currently selected in the tree view control.
-
-
ShowLines- Gets or sets a value indicating whether lines are drawn between tree nodes in the tree view control.
-
-
ShowNodeToolTips- Gets or sets a value indicating ToolTips are shown when the mouse pointer hovers over a TreeNode.
-
-
ShowPlusMinus- Gets or sets a value indicating whether plus-sign (+) and minus-sign (-) buttons are displayed next to tree nodes that contain child tree nodes.
-
-
ShowRootLines- Gets or sets a value indicating whether lines are drawn between the tree nodes that are at the root of the tree view.
-
-
TopNode- Gets the first fully-visible tree node in the tree view control.
-
-
VisibleCount- Gets the number of tree nodes that can be fully visible in the tree view control.
-
TreeView Tasks
When you select the TreeView and click on the Smart Tag icon
, a small menu appears with the list of tasks that can be achieved through it.
The only task that can be done is to edit the nodes of the TreeView.
TreeNode Editor
When you select the first item on the TreeView Tasks menu, the TreeNode Editor is displayed. This editor can also be accessed by clicking on the
button in the right hand column of the Nodes property in the Properties Window.
Clicking on 'Add Root' will add a root node (i.e., one with no parent) to the treeview. This can be seen above as Node0. Once the node has been added, then its properties can be changed.
Clicking 'Add Child' will add a new tree node to the collection as a child node under whatever node is currently selected. If you then click on the new node, its properties can be edited in the editor.
See TreeNode for details about their properties.
Clicking on the cross to the right of the "choose a node to edit" window, removes the highlighted node and any child nodes below it from your tree.
Adding nodes manually
Nodes can be added manually at both runtime and design time. The following code demonstrates how to add TreeNode objects through code to give the treeview shown at the top of this page:
treeView1.BeginUpdate(); treeView1.Nodes.Clear(); treeView1.Nodes.Add("Parent"); treeView1.Nodes[0].Nodes.Add("Child 1"); treeView1.Nodes[0].Nodes.Add("Child 2"); treeView1.Nodes[0].Nodes[1].Nodes.Add("Grandchild"); treeView1.Nodes[0].Nodes[1].Nodes[0].Nodes.Add("Great Grandchild"); treeView1.EndUpdate();
The first line of this code uses the method BeginUpdate() which disables any redrawing of the tree view.
The second line uses the method Clear() from the TreeNodeCollection class which clears the entire collection of tree nodes from a tree view.
The third line uses the method Add() from the TreeNodeCollection class which adds a new tree node with the specified label text to the end of the current tree node collection.
The next four lines add child tree nodes to the TreeNodeCollections of the parent nodes.
The final line uses the method EndUpdate() which enables any redrawing of the tree view.
Removing nodes manually
Nodes can be removed manually at both runtime and design time. The following code demonstrates how to remove a TreeNode object through code from the click of a button:
private void button1_Click(object sender, EventArgs e) { // If TreeNodeCollection is not read-only if (!treeView1.Nodes.IsReadOnly) { // Make sure a node is selected if (treeView1.SelectedNode != null) { TreeNode tn = treeView1.SelectedNode; treeView1.Nodes.Remove(tn); } } }
The Remove method takes a reference to a particular node as a parameter, so if you know the name of the node then it can simply be removed as follows:
treeView1.Nodes.Remove(Node0);
Alternatively you can remove a specified node by using the RemoveAt method. The following code demonstrates how to remove a specific TreeNode object through code from the click of a button:
private void button2_Click(object sender, EventArgs e) { // Delete the first TreeNode in the collection // if the Text property is "Node0" if(this.treeView1.Nodes[0].Text == "Node0") { this.treeView1.Nodes.RemoveAt(0); } }
Adding Images
Each node in the TreeView consists of two parts: an image and text. The image is optional; if you want the ability to attach an icon to a node, you start by first assigning an ImageList control to the TreeView control. Then you set the image associated with a node via the TreeNode Editor.
Adding Images manually
First you need to assign the TreeView.ImageList property to point to the instance of the ImageList component, if it has not been done in the TreeNode Editor.
this.treeView1.ImageList = myImageList;
Then you can assign an Image to a TreeNode. So in the above example, we could add the child node Child 1 in the following manner
treeView1.Nodes[0].Nodes.Add("Node0", "Child 1", 2);
Where 2 is the index in the ImageList.
MSDN references
|




