TreeView



TreeView

Tkinter Treeview is a module in python which is used for constructing some attractive GUI (Graphical User Interface) for the python developers. Tkinter Treeview is one of the standard libraries in python exclusively used for designing the user interfaces and provides web developers to play around with this tk based toolkit. NCBI Tree Viewer (TV) is the graphical display for phylogenetic trees. TV can visualize trees in ASN (text and binary), Newick and Nexus formats. To start using Tree Viewer go to the application homepage and look at some examples and demos. The following actions can be performed with a tree. Selects the specified tree-view item, scrolls the item into view, or redraws the item in the style used to indicate the target of a drag-and-drop operation. You can use this macro or the TreeViewSelectItem, TreeViewSelectSetFirstVisible, or TreeViewSelectDropTarget macros, or you can send the TVMSELECTITEM message explicitly. The disk space manager TreeSize Free tells you where precious disk space has gone. Visualize disk space usage with the treemap chart. Gain in-depth information in the column view. Filter scan results and break down information to better clean up your hard disk. The TreeView control provides a way to display information in a hierarchical structure by using collapsible nodes. This topic introduces the TreeView and TreeViewItem controls, and provides simple examples of their use. What Is a TreeView? TreeView is an ItemsControl that nests the items by using TreeViewItem controls.

TreeView

TreeView control in C# is used to display the items in hierarchical form. Each item in TreeView control is called a node. A node can have sub-nodes too; and each sub-node has it’s own nodes, and so on. All the nodes in the TreeView control are displayed in an hierarchical form, for better readability and control.

Through this article, we are going to discuss, using TreeView control in C# Windows applications. We will discuss, adding and removing nodes in TreeView control.

Add TreeView control to the Form

Create a C# Windows Forms Application in Visual Studio IDE. In Form’s design view, add the TreeView control from the Toolbox. Visual Studio automatically, creates a variable for this control and by using this we can use the features of the control.

You can add the nodes to the control in design view also, by selecting Nodes property from the Properties dialog. Through this Article, we are going to discuss the methods used to manage the control.

Adding nodes to the TreeView control

A Treeview control is the collection of nodes; each node is in the same level or in different hierarchical position in the tree structure. These collection of nodes are managed through it’s Nodes property.

Nodes is of TreeNodeCollection type, that represents the collection of tree nodes assigned to the TreeView control.

TreeNodeCollection has Add() method, which allows to add a new tree node to the end of the Nodes collection. Add() method has number of variances; these are used to add a node with label text, with key, with image index etc,.

Usually, we use the simple Add() method variance to add a new tree node with labeled text. Here is the code in C#;

TreeNode node = ctrlTreeView.Nodes.Add('Parent Node');

The above statement adds a new tree node, and returns the tree node that has added to the tree Nodes collection. The node is of TreeNode type.

We have another variance of Add() method, which takes a key and a label as arguments to add a new tree node. This method is more useful than the previous one. Key is used to search for the node, and it is assigned to TreeNode‘s Name property. And the label will be displayed as the TreeNode‘s Text. By adding, key to the node; enables to easily figure out the node from the Nodes collection. Below is an example, which adds a node with key & label. As discussed, label will be displayed, and key will be set to it’s Name property;

TreeNode node = ctrlTreeView.Nodes.Add('Parent', 'Parent Node');

Usually, it is good to give unique key values; to easily figure out the tree node from the collection of Nodes.

Adding sub-nodes to the TreeView control

When we add the node to the TreeView control; it returns the tree node that being added to the collection. By using the tree node, we can add sub-nodes to it. As I mentioned above, each tree node is of TreeNode type.

TreeNode type also has Nodes property (of TreeNodeCollection type) to manage the collection of nodes. Hence, we can use the same way (as above), to add the sub-nodes to the TreeNode. Below code adds the sub-node to the tree node; we got the tree node, from above statement;

TreeNode sub_node = node.Nodes.Add('Child-1', 'Sub Node');

CachedNCBI Tree Viewer

This way, we can add nodes & sub-nodes in different hierarchical level in TreeView control.

Removing a node from TreeView control

We can remove the node from tree nodes collection, by using it’s index or key; or different other ways. As we are using key, while adding the nodes; we will start using the method to removing the nodes using key value (its’ Name property value).

TreeNodeCollection class provides a RemoveByKey() method to remove the node by key value; that means by using TreeNode.Name property.

node.Nodes.RemoveByKey('Child-1');

The above code, removes the child node, what we have added in the previous section; from the tree nodes collection.

We can also remove the node from tree nodes collection, if we have the reference to TreeNode. As we have the reference of “Sub Node” in sub_node variable, below statement can directly removes the node from the collection.

node.Nodes.Remove(sub_node);

These methods will remove the node which is in current nodes collection.

We can also remove the node by calling it’s Remove() method. This is possible, only when we have the node reference.

sub_node.Remove();

Find a node in TreeView control

Treeview Tkinter

TreeView

To find a node by it’s key, we use TreeNodeCollection‘s Find() method. We need to specify the key value and whether to look into it’s sub-nodes also to search for the node.

Find() method returns the list of nodes which matches the given key value. Below is an example, which removes the first node which matches with the given key value.

node.Nodes.Find('key-value', true)[0].Remove();

Here is the complete working code, demonstrates adding and removing nodes from TreeView control;

The screenshot of it, looks like below;

How To Create A Tree View

(Raju)