As mentioned already, the nodes of a tree view are stored in a collection of type TreeNodeCollection. Every time you create a new node, it occupies a position inside the tree. Each node is represented by the Item indexed property of this collection. The first node of the tree has an index of 0. When you call the TreeNodeCollection::Add() member function to create a node, the new branch is added at the end of the list of its siblings. If you want, you can add a new child somewhere in the tree. To do this, you would call the TreeNodeCollection::Insert() member function. Its syntax is: public: virtual void Insert(int index, TreeNode^ node); The first argument to this member function is the index that the new node will occupy when created. The second argument is a reference to the new node to be created. Here is an example of using it: void InitializeComponent() { Text = "Countries Statistics"; Size = System::Drawing::Size(242, 320); tvwCountries = gcnew TreeView; tvwCountries->Location = Point(12, 12); tvwCountries->Width = 210; tvwCountries->Height = 100; array<TreeNode ^> ^ nodContinents = { gcnew TreeNode("Africa"), gcnew TreeNode("America"), gcnew TreeNode("Asia"), gcnew TreeNode("Europe") }; TreeNode ^ nodWorld = gcnew TreeNode("World", nodContinents); tvwCountries->Nodes->Add(nodWorld); tvwCountries->Nodes->Insert(1, gcnew TreeNode("Neptune")); Controls->Add(tvwCountries); } This would produce:
Another technique you can use to locate a node consists of using some coordinates. To do this, you can call the TreeView::GetNodeAt() member function that is overloaded with two versions whose syntaxes are: public: TreeNode^ GetNodeAt(Point pt); TreeNode^ GetNodeAt(int x, int y); To use this member function, you must know either the Point location or the x and y coordinates of the node. If you provide valid arguments to this member function, it returns a reference to the TreeNode located at the argument. Here is an example: void InitializeComponent() { Text = "Countries Statistics"; Size = System::Drawing::Size(242, 320); tvwCountries = gcnew TreeView; tvwCountries->Location = Point(12, 12); tvwCountries->Width = 210; tvwCountries->Height = 100; array<TreeNode ^> ^ nodContinents = { gcnew TreeNode("Africa"), gcnew TreeNode("America"), gcnew TreeNode("Asia"), gcnew TreeNode("Europe") }; TreeNode ^ nodWorld = gcnew TreeNode("World", nodContinents); tvwCountries->Nodes->Add(nodWorld); tvwCountries->ExpandAll(); TreeNode ^ nodBranch = tvwCountries->GetNodeAt(22, 48); Controls->Add(tvwCountries); Text = nodBranch->Text; } This would produce:
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: void InitializeComponent() { Text = "Countries Statistics"; Size = System::Drawing::Size(242, 320); tvwCountries = gcnew TreeView; tvwCountries->Location = Point(12, 12); tvwCountries->Width = 210; tvwCountries->Height = 100; array<TreeNode ^> ^ nodContinents = { gcnew TreeNode("Africa"), gcnew TreeNode("America"), gcnew TreeNode("Asia"), gcnew TreeNode("Europe") }; TreeNode ^ nodWorld = gcnew TreeNode("World", nodContinents); tvwCountries->Nodes->Add(nodWorld); TreeNode ^ nodFirst = tvwCountries->Nodes[0]->FirstNode; Controls->Add(tvwCountries); Text = nodFirst->Text; } To get a reference to the last child node, retrieve the TreeNode::LastNode property. You would use code as follows: TreeNode ^ nodLast = tvwCountries->Nodes[0]->LastNode; Text = nodLast->Text; 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. To find whether a tree view contains a certain node, you can call the TreeNodeCollection::Contains() member function. Its syntax is: public: bool Contains(TreeNode^ node); This member function expects as argument a reference to the node to look for. If the tree contains that node, the member function returns true. If the node is not found, this member function returns false.
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() member function. Its syntax is: public: void Remove(TreeNode ^ node); This member function expects a reference to the node you want to delete. Another solution you can use would consist of locating the node by its index. To do this, you would call the TreeNodeCollection.RemoveAt() member function. Its syntax is: public: virtual void RemoveAt(int index); When calling this member function, 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() member function. Its syntax is: public: void Remove(); One of the characteristics of a tree in the real world is that, if you cut a branch, the branches attached to it and their leaves are cut too. In the same way, if you call any of these Remove() or RemoveAt() member functions to delete a node, its children, if any, would be deleted too. To remove all nodes of a tree view, you can call the TreeNodeCollection::Clear() member function. Its syntax is: public: virtual void Clear(); This member function is used to get rid of all nodes of a tree.
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: void InitializeComponent() { Text = "Countries Statistics"; Size = System::Drawing::Size(242, 320); tvwCountries = gcnew TreeView; tvwCountries->Location = Point(12, 12); tvwCountries->Width = 210; tvwCountries->Height = 100; tvwCountries->HotTracking = true; Controls->Add(tvwCountries); array<TreeNode ^> ^ nodContinents = { gcnew TreeNode("Africa"), gcnew TreeNode("America"), gcnew TreeNode("Asia"), gcnew TreeNode("Europe") }; TreeNode ^ nodWorld = gcnew 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: void InitializeComponent() { Text = "Countries Statistics"; Size = System::Drawing::Size(242, 320); tvwCountries = gcnew TreeView; tvwCountries->Location = Point(12, 12); tvwCountries->Width = 210; tvwCountries->Height = 100; tvwCountries->ShowLines = false; array<TreeNode ^> ^ nodContinents = { gcnew TreeNode("Africa"), gcnew TreeNode("America"), gcnew TreeNode("Asia"), gcnew TreeNode("Europe") }; TreeNode ^ nodWorld = gcnew 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: void InitializeComponent() { Text = "Countries Statistics"; Size = System::Drawing::Size(242, 320); tvwCountries = gcnew TreeView; tvwCountries->Location = Point(12, 12); tvwCountries->Width = 210; tvwCountries->Height = 100; tvwCountries->HotTracking = true; tvwCountries->ShowLines = false; tvwCountries->FullRowSelect = true; array<TreeNode ^> ^ nodContinents = { gcnew TreeNode("Africa"), gcnew TreeNode("America"), gcnew TreeNode("Asia"), gcnew TreeNode("Europe") }; TreeNode ^ nodWorld = gcnew 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() member function. Its syntax is: public: void Expand(); This member function 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() member function. 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() member function. 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() member function 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() member function. 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: void InitializeComponent() { Text = "Countries Statistics"; Size = System::Drawing::Size(242, 320); tvwCountries = gcnew TreeView; tvwCountries->Location = Point(12, 12); tvwCountries->Width = 210; tvwCountries->Height = 100; tvwCountries->CheckBoxes = true; array<TreeNode ^> ^ nodContinents = { gcnew TreeNode("Africa"), gcnew TreeNode("America"), gcnew TreeNode("Asia"), gcnew TreeNode("Europe") }; TreeNode ^ nodWorld = gcnew 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:
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, array<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() member function. In the same way, you can create an array of TreeNode objects and pass it to the TreeNodeCollection::AddRange() member function. |
|
|||||||||||||||||||||||||||||
|