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

No comments:

Post a Comment