TreeView Control in Visual Basic 6.0


The TreeView control display a hirearchical display node object.Each node consist of label ,optional bitmap.You use this control to display the information in hierarchical tree.

The TreeView control is designed to display data that is hierarchical in nature, such as organization trees, the entries in an index, the files and directories on a disk.

Uses of TreeView

  • To create an organization tree that can be manipulated by the user.
  • To create a tree that shows at least two or more levels of a database.

Tree Trems

Node: A node can be best thought of as a branch on our tree. Each node represents 1 item. A node can hold any number of sub nodes.

Sibling: A sibling is another node that exists on the same branch as the referring node.

Children: Children are sub-nodes of the current node. Sometimes called Child nodes.

Parent: A parent node is the node “up” from your current node. All nodes except the root node have a parent node.

 

Adding Nodes To The TreeView

 

Object Type Object Name Instructions
Treeview Treeview1 The Treeview control where it all happens. You’ll be using this control though the whole tutorial.
Imagelist ImageList1 This control will hold the images for our nodes. Add 3 images to the image list control.

Image Key
Image of a Closed Folder Closed
Image of an Open Folder Open
Image of a File Leaf

The Treeview Control holds a collection of nodes so to add a node to the Tree you need to define a new node and then add that node to the Treeview’s collection. Like this:

Dim nodxy As Node
‘Add root Node
Set nodxy = TreeView1.Nodes.Add(, , “Root”, “Root Node”)

 

Properties of the TreeView Control

 

The following are some of the commonly used properties of the TreeView control:

 

S.N Property Description
1 BackColor Gets or sets the background color for the control.
2 BackgroundImage Gets or set the background image for the TreeView control.
3 BackgroundImageLayout Gets or sets the layout of the background image for the TreeView control.
4 BorderStyle Gets or sets the border style of the tree view control.
5 CheckBoxes Gets or sets a value indicating whether check boxes are displayed next to the tree nodes in the tree view control.
6 DataBindings Gets the data bindings for the control.
7 Font Gets or sets the font of the text displayed by the control.
8 FontHeight Gets or sets the height of the font of the control.
9 ForeColor The current foreground color for this control, which is the color the control uses to draw its text.
10 ItemHeight Gets or sets the height of each tree node in the tree view control.
11 Nodes Gets the collection of tree nodes that are assigned to the tree view control.
12 PathSeparator Gets or sets the delimiter string that the tree node path uses.
13 RightToLeftLayout Gets or sets a value that indicates whether the TreeView should be laid out from right-to-left.
14 Scrollable Gets or sets a value indicating whether the tree view control displays scroll bars when they are needed.
15 SelectedImageIndex Gets or sets the image list index value of the image that is displayed when a tree node is selected.
16 SelectedImageKey Gets or sets the key of the default image shown when a TreeNode is in a selected state.
17 SelectedNode Gets or sets the tree node that is currently selected in the tree view control.
18 ShowLines Gets or sets a value indicating whether lines are drawn between tree nodes in the tree view control.
19 ShowNodeToolTips Gets or sets a value indicating ToolTips are shown when the mouse pointer hovers over a TreeNode.
20 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.
21 ShowRootLines Gets or sets a value indicating whether lines are drawn between the tree nodes that are at the root of the tree view.
22 Sorted Gets or sets a value indicating whether the tree nodes in the tree view are sorted.
23 StateImageList Gets or sets the image list that is used to indicate the state of the TreeView and its nodes.
24 Text Gets or sets the text of the TreeView.
25 TopNode Gets or sets the first fully-visible tree node in the tree view control.
26 TreeViewNodeSorter Gets or sets the implementation of IComparer to perform a custom sort of the TreeView nodes.
27 VisibleCount Gets the number of tree nodes that can be fully visible in the tree view control.

Methods of TreeView Control

The following are some of the commonly used methods of the TreeView control:

S.N Method Name Descriptrion
1. CollapseAll Collapses all the nodes including all child nodes in the tree view control.
2. ExpandAll Expands all the node.
3. GetNodeAt Gets the node at the specified location.
4. GetNodeCount Gets the number of tree nodes.
5. Sort Sorts all the items in the tree view control.
6. ToString Returns a string containing the name of the control.

 

Events of the TreeView Control

The following are some of the commonly used events of the TreeView control:

S.N Event Description
1 AfterCheck Occurs after the tree node check box is checked.
2 AfterCollapse Occurs after the tree node is collapsed.
3 AfterExpand Occurs after the tree node is expanded.
4 AfterSelect Occurs after the tree node is selected.
5 BeforeCheck Occurs before the tree node check box is checked.
6 BeforeCollapse Occurs before the tree node is collapsed.
7 BeforeExpand Occurs before the tree node is expanded.
8 BeforeLabelEdit Occurs before the tree node label text is edited.
9 BeforeSelect Occurs before the tree node is selected.
10 ItemDrag Occurs when the user begins dragging a node.
11 NodeMouseClick Occurs when the user clicks a TreeNode with the mouse.
12 NodeMouseDoubleClick Occurs when the user double-clicks a TreeNode with the mouse.
13 NodeMouseHover Occurs when the mouse hovers over a TreeNode.
14 PaddingChanged Occurs when the value of the Padding property changes.
15 Paint Occurs when the TreeView is drawn.
16 RightToLeftLayoutChanged Occurs when the value of the RightToLeftLayout property changes.
17 TextChanged Occurs when the Text property changes.

The TreeNode Class

The TreeNode class represents a node of a TreeView. Each node in a TreeView control is an object of the TreeNode class. To be able to use a TreeView control we need to have a look at some commonly used properties and methods of the TreeNode class.

Properties of the TreeNode Class

The following are some of the commonly used properties of the TreeNode class:

S.N Property Description
1 BackColor Gets or sets the background color of the tree node.
2 Checked Gets or sets a value indicating whether the tree node is in a checked state.
3 ContextMenu Gets the shortcut menu that is associated with this tree node.
4 ContextMenuStrip Gets or sets the shortcut menu associated with this tree node.
5 FirstNode Gets the first child tree node in the tree node collection.
6 FullPath Gets the path from the root tree node to the current tree node.
7 Index Gets the position of the tree node in the tree node collection.
8 IsEditing Gets a value indicating whether the tree node is in an editable state.
9 IsExpanded Gets a value indicating whether the tree node is in the expanded state.
10 IsSelected Gets a value indicating whether the tree node is in the selected state.
11 IsVisible Gets a value indicating whether the tree node is visible or partially visible.
12 LastNode Gets the last child tree node.
13 Level Gets the zero-based depth of the tree node in the TreeView control.
14 Name Gets or sets the name of the tree node.
15 NextNode Gets the next sibling tree node.
16 Nodes Gets the collection of TreeNode objects assigned to the current tree node.
17 Parent Gets the parent tree node of the current tree node.
18 PrevNode Gets the previous sibling tree node.
19 PrevVisibleNode Gets the previous visible tree node.
20 Tag Gets or sets the object that contains data about the tree node.
21 Text Gets or sets the text displayed in the label of the tree node.
22 ToolTipText Gets or sets the text that appears when the mouse pointer hovers over a TreeNode.
23 TreeView Gets the parent tree view that the tree node is assigned to.

Methods of the TreeNode Class

The following are some of the commonly used methods of the TreeNode class:

S.N Method Name Descriptrion
1. Collapse Collapses the tree node.
2. Expand Expands the tree node.
3. ExpandAll Expands all the child tree nodes.
4. GetNodeCount Returns the number of child tree nodes.
5. Remove Removes the current tree node from the tree view control..
6. Toggle Toggles the tree node to either the expanded or collapsed state.
7. ToString Returns a string that represents the current object

 

Example

In this example, let us create a tree view at runtime.

 

1.Click on “Start” button . Select  “All Programs”,” Microsoft Visual Studio 6.0″ and  Microsoft Visual Basic 6.0.” .

 

2. Select “Standard EXE” .From the New Project dialog box  click on “Open.” Click on “Project” on the menu bar, then select “Components” from the drop-down menu.

 

3. Scroll down the list in the box until Microsoft Windows Common Controls 6.0 (SP4) is visible. Click on the checkbox to select the component and then click on “OK.” All components show in the Toolbox.

 

4. Choose the “TreeView” control from the list of controls in the toolbox. Place the control on the form in Visual Basic.

 

 

 

5. Double click on TreeView control.Code window is open.Write code here as shown in figure below:

 

 

 

 

 

 

 

 

 

 

 

6.  To Run the program press <f5>.  Double click on font,style,size  node is open and output is:

 

 

 

 

 

Example

In this example, let us create a tree view at runtime. Now double click on the Form and put the follow code in the coding window.

Public Class Form1Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

‘create a new TreeView

Dim TreeView1 As TreeView

TreeView1 = New TreeView()

TreeView1.Location = New Point(20, 20)

TreeView1.Size = New Size(170, 170)

Me.Controls.Add(TreeView1)

TreeView1.Nodes.Clear()

Creating the root node

Dim root = New TreeNode(“Project”)

TreeView1.Nodes.Add(root)

TreeView1.Nodes(0).Nodes.Add(New TreeNode(“Project 1”))

‘Creating child nodes under the first child

For index1 As Integer = 1 To 4

TreeView1.Nodes(0).Nodes(0).Nodes.Add(New _  TreeNode(“Form” & Str(index1)))

Next index1

creating child nodes under the root

TreeView1.Nodes(0).Nodes.Add(New TreeNode(“Project 2”))

‘creating child nodes under the created child node

For index1 As Integer = 1 To 3

TreeView1.Nodes(0).Nodes(1).Nodes.Add(New _TreeNode(“Form” & Str(loopindex)))

Next index1

Set the caption bar text of the form.

Me.Text=”ComputerNotes.in”

EndSub

EndClass

When the above code is executed and run using Start button available at the Microsoft Visual Studio tool bar, it will show the following window:

Comments are closed.