|
TreeView
The tree view control is used to display a hierarchy of nodes (both parent, child).
You can expand and collpase these nodes by clicking them. This control is similar
to Windows Explorer which displays a tree view in it's left pane to list all the folders
on the hard disk. Below is the image of a Tree View control.
Notable Properties of TreeView
Bounds: Gets the actual bound of the tree node
Checked: Gets/Sets whether the tree node is checked
FirstNode: Gets the first child tree node
FullPath: Gets the path from the root node to the current
node
ImageIndex: Gets/Sets the image list index of the image
displayed for a node
Index: Gets the location of the node in the node collection
IsEditing: Gets whether the node can be edited
IsExpaned: Gets whether the node is expaned
IsSelected: Gets whether the node is selected
LastNode: Gets the last child node
NextNode: Gets the next sibling node
NextVisibleNode: Gets the next visible node
NodeFont: Gets/Sets the font for nodes
Nodes: Gets the collection of nodes in the current node
Parent: Gets the parent node of the current node
PrevNode: Gets the previous sibling node
PrevVisibleNode: Gets the previous visible node
TreeView: Gets the node's parent tree view
TreeView Event
Default event of the Tree View control is the AfterSelect event which looks like this
in code:
Private Sub TreeView1_AfterSelect(ByVal sender As System.Object, ByVal e As_
System.Windows.Forms.TreeViewEventArgs) Handles TreeView1.AfterSelect
End Sub
|
Working with Tree Views
Drag a Tree View control on to a form and to add nodes to it select the nodes property
in the properties window, which displays the TreeNode editor as shown below.
To start adding nodes, you should click the Add Root button, which adds a top-level
node. To add child nodes to that node, you should select that node and use the Add
Child button. To set text for a node, select the node and set it's text
in the textbox as shown in the image above.
Assuming you added some nodes to the tree view, drag two Labels (Label1, Label2) from
the toolbox on to the form. The following code displays the node you select on
Label2 and the path to that node on Label1. The code looks like this:
Public Class Form12 Inherits System.Windows.Forms.Form
#Region " Windows Form Designer generated code "
#End Region
Private Sub TreeView1_AfterSelect(ByVal sender As System.Object, ByVal e As_
System.Windows.Forms.TreeViewEventArgs) Handles TreeView1.AfterSelect
Label1.Text = "You are here->" & " " & e.Node.FullPath
'displaying the path of the selected node
Label2.Text = "Current node selected:" & " " & e.Node.Text
'displaying the selected node
End Sub
End Class
|
The image below displays sample output from above code.
|