Showing posts with label VB.Net Basic Controls. Show all posts
Showing posts with label VB.Net Basic Controls. Show all posts

Saturday, 14 September 2013

VB.Net - ListView Control

The ListView control is used to display a list of items. Along with the TreeView control, it allows you to create a Windows Explorer like interface.
Let's click on a ListView control from the Toolbox and place it on the form.
VB.Net ListView Control
The ListView control displays a list of items along with icons. The Item property of the ListView control allows you to add and remove items from it. The SelectedItem property contains a collection of the selected items. The MultiSelect property allows you to set select more than one item in the list view. The CheckBoxes property allows you to set check boxes next to the items.

Properties of the ListView Control

The following are some of the commonly used properties of the ListView control:
S.NPropertyDescription
1AlignmentGets or sets the alignment of items in the control.
2AutoArrangeGets or sets whether icons are automatically kept arranged.
3BackColorGets or sets the background color.
4CheckBoxesGets or sets a value indicating whether a check box appears next to each item in the control.
5CheckedIndicesGets the indexes of the currently checked items in the control.
6CheckedItemsGets the currently checked items in the control.
7ColumnsGets the collection of all column headers that appear in the control.
8GridLinesGets or sets a value indicating whether grid lines appear between the rows and columns containing the items and subitems in the control.
9HeaderStyleGets or sets the column header style.
10HideSelectionGets or sets a value indicating whether the selected item in the control remains highlighted when the control loses focus.
11HotTrackingGets or sets a value indicating whether the text of an item or subitem has the appearance of a hyperlink when the mouse pointer passes over it.
12HoverSelectionGets or sets a value indicating whether an item is automatically selected when the mouse pointer remains over the item for a few seconds.
13InsertionMarkGets an object used to indicate the expected drop location when an item is dragged within a ListView control.
14ItemsGets a collection containing all items in the control.
15LabelWrapGets or sets a value indicating whether item labels wrap when items are displayed in the control as icons.
16LargeImageListGets or sets the ImageList to use when displaying items as large icons in the control.
17MultiSelectGets or sets a value indicating whether multiple items can be selected.
18RightToLeftLayoutGets or sets a value indicating whether the control is laid out from right to left.
19ScrollableGets or sets a value indicating whether a scroll bar is added to the control when there is not enough room to display all items.
20SelectedIndicesGets the indexes of the selected items in the control.
21SelectedItemsGets the items that are selected in the control.
22ShowGroupsGets or sets a value indicating whether items are displayed in groups.
23ShowItemToolTipsGets or sets a value indicating whether ToolTips are shown for the ListViewItem objects contained in theListView.
24SmallImageListGets or sets the ImageList to use when displaying items as small icons in the control.
25SortingGets or sets the sort order for items in the control.
26StateImageListGets or sets the ImageList associated with application-defined states in the control.
27TopItemGets or sets the first visible item in the control.
28ViewGets or sets how items are displayed in the control. This property has the following values:
  • LargeIcon - displays large items with a large 32 x 32 pixels icon.
  • SmallIcon - displays items with a small 16 x 16 pixels icon
  • List - displays small icons always in one column
  • Details - displays items in multiple columns with column headers and fields
  • Tile - displays items as full-size icons with item labels and sub-item information.
29VirtualListSizeGets or sets the number of ListViewItem objects contained in the list when in virtual mode.
30VirtualModeGets or sets a value indicating whether you have provided your own data-management operations for the ListView control.

Methods of the ListView Control

The following are some of the commonly used methods of the ListView control:
S.NMethod Name & Description
1Clear 
Removes all items from the ListView control.
1ToString 
Returns a string containing the string representation of the control.

Events of the ListView Control

The following are some of the commonly used events of the ListView control:
S.NEventDescription
1ColumnClickOccurs when a column header is clicked.
2ItemCheckOccurs when an item in the control is checked or unchecked.
3SelectedIndexChangedOccurs when the selected index is changed.
4TextChangedOccurs when the Text property is changed.

Example

In this example, let us create a list 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 ListView
      Dim ListView1 As ListView
      ListView1 = New ListView()
      ListView1.Location = New Point(10, 10)
      ListView1.Size = New Size(150, 150)
      Me.Controls.Add(ListView1)
      'Creating the list items
      Dim ListItem1 As ListViewItem
      ListItem1 = ListView1.Items.Add("Item 1")
      Dim ListItem2 As ListViewItem
      ListItem2 = ListView1.Items.Add("Item 2")
      Dim ListItem3 As ListViewItem
      ListItem3 = ListView1.Items.Add("Item 3")
      Dim ListItem4 As ListViewItem
      ListItem4 = ListView1.Items.Add("Item 4")
      'set the view property
      ListView1.View = View.SmallIcon
      ' 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:
ListView Example

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