![]() |
Characteristics of a Tree View |
|
The Path to a Node |
|
After a node has been added to a tree, it holds a position relative to its parent and its existence depends on that parent. To keep track of its "ancestry", each node has a path that can be used to identify its parent and its grand-parent(s), if any. To know the path of a node from itself to the root, you can access its TreeNode.FullPath property. This property produces a string made of sections separated by a specific character identified as the TreeView.PathSeparator property. |
|
By default, this character is the backslash, following the conventions of the operating system. If you want to use a different character or string, assign it to the PathSeparator property. To know what character or string a tree view is using as the separator, you can retrieve the value of its PathSeparator property.
In order to select an item, the user must click it or navigate to it using the keyboard. Alternatively, if you want the items to be underlined when the mouse passes over them, set to true the TreeView.HotTracking Boolean property. Its default value is false. Here is an example: private void InitializeComponent()
{
Text = "Countries Statistics";
Size = new Size(242, 320);
tvwCountries = new TreeView();
tvwCountries.Location = new Point(12, 12);
tvwCountries.Width = 210;
tvwCountries.Height = 100;
tvwCountries.HotTracking = true;
Controls.Add(tvwCountries);
TreeNode[] nodContinents = { new TreeNode("Africa"),
new TreeNode("America"),
new TreeNode("Asia"),
new TreeNode("Europe") };
TreeNode nodWorld = new TreeNode("World", nodContinents);
tvwCountries.Nodes.Add(nodWorld);
}
This would produce:
As mentioned already, a tree view appears as a list of items arranged like a tree. This implies a relationship of parent-child among the items in the control. To indicate this relationship between two nodes, a line is drawn from one to another. Based on this, a line from a node on top of another node under it indicates that the one on top is the parent to the one under it. 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 (in the .NET Framework, some other libraries have it set by default to false). If this property is set to false, the lines between the nodes would not display. Here is an example: private void InitializeComponent()
{
Text = "Countries Statistics";
Size = new Size(242, 320);
tvwCountries = new TreeView();
tvwCountries.Location = new Point(12, 12);
tvwCountries.Width = 210;
tvwCountries.Height = 100;
tvwCountries.ShowLines = false;
TreeNode[] nodContinents = { new TreeNode("Africa"),
new TreeNode("America"),
new TreeNode("Asia"),
new TreeNode("Europe") };
TreeNode nodWorld = new TreeNode("World", nodContinents);
tvwCountries.Nodes.Add(nodWorld);
Controls.Add(tvwCountries);
}
This would produce:
If you create a tree that has more than one root, a line is drawn among those root nodes. Here is an example: ![]() The presence or absence of this type of line is controlled by the TreeView.ShowRootLines Boolean property.
Indentation is the ability for a child node to be aligned to the right with regards to its parent. The general distance from the left border of the parent to the left border of the child is partially controlled by the TreeView.Indent property which is an integer. If the default distance doesn't suit you, you can change it by assigning a positive number to the control's Indent property.
When the user clicks an item, that node becomes highlighted for the length of its string. If you want, you can show the highlighting on the selected node but from the left to the right borders of the tree view. To do this, you can set the TreeView.FullRowSelect Boolean property to true. Its default value is false. For the TreeView.FullRowSelect property to work, the ShowLines property must be set to false. Here is an example: private void InitializeComponent()
{
Text = "Countries Statistics";
Size = new Size(242, 320);
tvwCountries = new TreeView();
tvwCountries.Location = new Point(12, 12);
tvwCountries.Width = 210;
tvwCountries.Height = 100;
tvwCountries.HotTracking = true;
tvwCountries.ShowLines = false;
tvwCountries.FullRowSelect = true;
TreeNode[] nodContinents = { new TreeNode("Africa"),
new TreeNode("America"),
new TreeNode("Asia"),
new TreeNode("Europe") };
TreeNode nodWorld = new TreeNode("World", nodContinents);
tvwCountries.Nodes.Add(nodWorld);
Controls.Add(tvwCountries);
}
This would produce:
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.
At this time, we have seen that some nodes have children and some don't. When a node has at least one child, the node indicates this by displaying a + button. If the user clicks the + button, the node expands, displays a list of its children, and the button becomes -. The presence or absence of the + and - buttons is controlled by the TreeView.ShowPlusMinus Boolean property. By default, this property is set to true. If you don't want the parent nodes to display the + or - button, set this property to false.
When a node displays a + button and the user clicks that button, the node displays its child(ren). This action is referred to as expanding the node. To programmatically expand a node, call its TreeNode.Expand() method. Its syntax is: public void Expand(); This method only expands the node that calls it but if its children have their own children, they are not expanded. To expand a node and its children that have nodes, you can call its TreeNode.ExpandAll() method. Its syntax is: public void ExpandAll(); 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: public void ExpandAll(); 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: public void Collapse(); 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: public void CollapseAll();
Besides the strings (and some small pictures as we will see later), the nodes of a tree view can display a check box on their left side. The presence or absence of the check box is controlled by the CheckBoxes Boolean property whose default value is false. If you want to display the check boxes, set this property to true. Here is an example: private void InitializeComponent()
{
Text = "Countries Statistics";
Size = new Size(242, 320);
tvwCountries = new TreeView();
tvwCountries.Location = new Point(12, 12);
tvwCountries.Width = 210;
tvwCountries.Height = 100;
tvwCountries.CheckBoxes = true;
TreeNode[] nodContinents = { new TreeNode("Africa"),
new TreeNode("America"),
new TreeNode("Asia"),
new TreeNode("Europe") };
TreeNode nodWorld = new TreeNode("World", nodContinents);
tvwCountries.Nodes.Add(nodWorld);
Controls.Add(tvwCountries);
}
This would produce:
If you equip the nodes with check boxes, the user can click an item to select it independently of the check box. The user can also click the check box, which would place a check mark in the box. To programmatically check the box, you can assign a true value to the node's Checked property. When a check mark has been placed in a node's check box, the tree view fires an AfterCheck event, which is handled by the TreeViewEventHandler delegate. The AfterCheck event is carried by the TreeViewEventArgs class. One of properties of this class is called Action, which specifies why or how the event occurred. The Action property is in fact a value based on the TreeViewAction enumerator. Its members are: ByKeyboard: This indicates that the event was fired by pressing a key ByMouse: This indicates that the event was fired based on an action on the mouse Collapse: This indicates that event was fired when the tree collapsed Expand: This indicates that the event was fired when the tree expanded Unknown: None of the above reasons caused the event, but the event was fired The other property of the TreeViewEventArgs class is called Node. This member is of type TreeNode. It carries a reference to the node that fired the event, whether it was clicked, checked, expanded, or collapsed. To programmatically find out if a node is checked, check the value of its Checked property.
Each of the nodes we have used so far displayed a simple piece of text. To enhance the appearance of a node, besides its text, you can display a small icon to the left of its string. To do this, you must first create an ImageList control and assign it to the TreeView.ImageList property. When creating a node, if you plan to display an icon next to it, you can use the following constructor of the TreeNode class: public TreeNode(String text, int imageIndex, int selectedImageIndex); 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: public TreeNode(string text,
int imageIndex,
int selectedImageIndex,
TreeNode children[]);
Just as done previously, after defining the TreeNode object, you can add it to the tree by passing it to the TreeNodeCollection.Add() method. In the same way, you can create an array of TreeNode objects and pass it to the TreeNodeCollection.AddRange() method.
|
|
|
||
| Previous | Copyright © 2007 FunctionX, Inc. | Home |
|
|
||