Saturday 14 September 2013

VB.Net - TreeView Control

The TreeView control is used to display hierarchical representations of items similar to the ways the files and folders are displayed in the left pane of the Windows Explorer. Each node may contain one or more child nodes.
Let's click on a TreeView control from the Toolbox and place it on the form.
VB.Net TreeView Control

Properties of the TreeView Control

The following are some of the commonly used properties of the TreeView control:
S.NPropertyDescription
1BackColorGets or sets the background color for the control.
2BackgroundImageGets or set the background image for the TreeView control.
3BackgroundImageLayoutGets or sets the layout of the background image for the TreeView control.
4BorderStyleGets or sets the border style of the tree view control.
5CheckBoxesGets or sets a value indicating whether check boxes are displayed next to the tree nodes in the tree view control.
6DataBindingsGets the data bindings for the control.
7FontGets or sets the font of the text displayed by the control.
8FontHeightGets or sets the height of the font of the control.
9ForeColorThe current foreground color for this control, which is the color the control uses to draw its text.
10ItemHeightGets or sets the height of each tree node in the tree view control.
11NodesGets the collection of tree nodes that are assigned to the tree view control.
12PathSeparatorGets or sets the delimiter string that the tree node path uses.
13RightToLeftLayoutGets or sets a value that indicates whether the TreeView should be laid out from right-to-left.
14ScrollableGets or sets a value indicating whether the tree view control displays scroll bars when they are needed.
15SelectedImageIndexGets or sets the image list index value of the image that is displayed when a tree node is selected.
16SelectedImageKeyGets or sets the key of the default image shown when a TreeNode is in a selected state.
17SelectedNodeGets or sets the tree node that is currently selected in the tree view control.
18ShowLinesGets or sets a value indicating whether lines are drawn between tree nodes in the tree view control.
19ShowNodeToolTipsGets or sets a value indicating ToolTips are shown when the mouse pointer hovers over a TreeNode.
20ShowPlusMinusGets or sets a value indicating whether plus-sign (+) and minus-sign (-) buttons are displayed next to tree nodes that contain child tree nodes.
21ShowRootLinesGets or sets a value indicating whether lines are drawn between the tree nodes that are at the root of the tree view.
22SortedGets or sets a value indicating whether the tree nodes in the tree view are sorted.
23StateImageListGets or sets the image list that is used to indicate the state of the TreeView and its nodes.
24TextGets or sets the text of the TreeView.
25TopNodeGets or sets the first fully-visible tree node in the tree view control.
26TreeViewNodeSorterGets or sets the implementation of IComparer to perform a custom sort of the TreeView nodes.
27VisibleCountGets the number of tree nodes that can be fully visible in the tree view control.

Methods of the TreeView Control

The following are some of the commonly used methods of the TreeView control:
S.NMethod Name & Description
1CollapseAll
Collapses all the nodes including all child nodes in the tree view control.
2ExpandAll
Expands all the nodes.
3GetNodeAt
Gets the node at the specified location.
4GetNodeCount
Gets the number of tree nodes.
5Sort 
Sorts all the items in the tree view control.
6ToString 
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.NEventDescription
1AfterCheckOccurs after the tree node check box is checked.
2AfterCollapseOccurs after the tree node is collapsed.
3AfterExpandOccurs after the tree node is expanded.
4AfterSelectOccurs after the tree node is selected.
5BeforeCheckOccurs before the tree node check box is checked.
6BeforeCollapseOccurs before the tree node is collapsed.
7BeforeExpandOccurs before the tree node is expanded.
8BeforeLabelEditOccurs before the tree node label text is edited.
9BeforeSelectOccurs before the tree node is selected.
10ItemDragOccurs when the user begins dragging a node.
11NodeMouseClickOccurs when the user clicks a TreeNode with the mouse.
12NodeMouseDoubleClickOccurs when the user double-clicks a TreeNode with the mouse.
13NodeMouseHoverOccurs when the mouse hovers over a TreeNode.
14PaddingChangedOccurs when the value of the Padding property changes.
15PaintOccurs when the TreeView is drawn.
16RightToLeftLayoutChangedOccurs when the value of the RightToLeftLayout property changes.
17TextChangedOccurs 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.NPropertyDescription
1BackColorGets or sets the background color of the tree node.
2CheckedGets or sets a value indicating whether the tree node is in a checked state.
3ContextMenuGets the shortcut menu that is associated with this tree node.
4ContextMenuStripGets or sets the shortcut menu associated with this tree node.
5FirstNodeGets the first child tree node in the tree node collection.
6FullPathGets the path from the root tree node to the current tree node.
7IndexGets the position of the tree node in the tree node collection.
8IsEditingGets a value indicating whether the tree node is in an editable state.
9IsExpandedGets a value indicating whether the tree node is in the expanded state.
10IsSelectedGets a value indicating whether the tree node is in the selected state.
11IsVisibleGets a value indicating whether the tree node is visible or partially visible.
12LastNodeGets the last child tree node.
13LevelGets the zero-based depth of the tree node in the TreeView control.
14NameGets or sets the name of the tree node.
15NextNodeGets the next sibling tree node.
16NodesGets the collection of TreeNode objects assigned to the current tree node.
17ParentGets the parent tree node of the current tree node.
18PrevNodeGets the previous sibling tree node.
19PrevVisibleNodeGets the previous visible tree node.
20TagGets or sets the object that contains data about the tree node.
21TextGets or sets the text displayed in the label of the tree node.
22ToolTipTextGets or sets the text that appears when the mouse pointer hovers over a TreeNode.
23TreeViewGets 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.NMethod Name & Description
1Collapse
Collapses the tree node.
2Expand
Expands the tree node.
3ExpandAll
Expands all the child tree nodes.
4GetNodeCount
Returns the number of child tree nodes.
5Remove 
Removes the current tree node from the tree view control.
6Toggle
Toggles the tree node to either the expanded or collapsed state.
7ToString 
Returns a string that represents the current object.

Example

In this example, let us create a tree view at runtime. Let's double click on the Form and put the follow code in the opened window.
Public Class Form1
   Private 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(10, 10)
      TreeView1.Size = New Size(150, 150)
      Me.Controls.Add(TreeView1)
      TreeView1.Nodes.Clear()
      'Creating the root node
      Dim root = New TreeNode("Application")
      TreeView1.Nodes.Add(root)
      TreeView1.Nodes(0).Nodes.Add(New TreeNode("Project 1"))
      'Creating child nodes under the first child
      For loopindex As Integer = 1 To 4
          TreeView1.Nodes(0).Nodes(0).Nodes.Add(New  _
              TreeNode("Sub Project" & Str(loopindex)))
      Next loopindex
      ' creating child nodes under the root
      TreeView1.Nodes(0).Nodes.Add(New TreeNode("Project 6"))
      'creating child nodes under the created child node
      For loopindex As Integer = 1 To 3
          TreeView1.Nodes(0).Nodes(1).Nodes.Add(New  _
              TreeNode("Project File" & Str(loopindex)))
      Next loopindex
      ' Set the caption bar text of the form.  
      Me.Text = "tutorialspoint.com"
   End Sub
End Class
When the above code is executed and run using Start button available at the Microsoft Visual Studio tool bar, it will show following window:
Treeview Example
You can expand the nodes to see the child nodes:
TreeView Result

No comments:

Post a Comment