ListView Control in VB.net – Properties, Methods and Events

What is ListView Control in VB.net?

In Windows Forms, the ListView Control in VB.net are used to show a group of items. It uses one of the view lists, like LargeIcon, SmallIcon, Details, List, or Tile. The ListView in VB.net also lets the user add or take away icons from the ListView Control.

See also all the Basic Control Using VB.net with Source Code….

In order for you to test your VB.net Code provided in this lesson, you must test the code in your code editor. But if you wish to run this code online, we also have an Online Compiler in VB.net for you to test your VB.net Code for free.

We have learned how to use the TreeView Control in VB.net in the previous lesson. In this lesson we shall learn How To Write a Program for ListView in VB.net.

Let’s create a ListView in VB.net by dragging a ListView Control from the Toolbox and dropping it on the form.

ListView Control in VB.net Tutorial
ListView Control in VB.net Tutorial

ListView shows a list of items with icons next to them. You can add and remove items from a ListView control by using its Item property. The SelectedItem property has a list of the items that have been chosen. You can choose more than one item in the list view by using the MultiSelect property. You can put check boxes next to the items with the CheckBoxes property.

ListView Control in VB.net Properties

The following given below are some commonly used Properties of the ListView Control in VB.net.

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

ListView Control in VB.net Methods

The following given below are some commonly used Methods of the ListView Control in VB.net.

#ListView Control in VB.net MethodsDescription
1.ArrangeIcons()The ArrangeIcons method is used to arrange all the items displayed as icons in the ListView control.
2.FindItemWithText()It is used to search the first ListViewItem that started with the given text value.
3.GetItemAt()The GetItemAt method is used to get an item at the specified location of the ListView control.
4.Clear()The Clear method is used to clear all the items and columns from the ListView control.
5.Sort()The Sort method is used to sort items of the ListView.
Methods and Description of ListView Control in VB.net

ListView Control in VB.net Events

The following given below are some commonly used Events of the ListView Control in VB.net.

#ListView Control in VB.net EventsDescription
1.ItemActivateThe ItemActivate event occurred when an item activated in the ListView control.
2.ItemCheckedThe ItemChecked event is found in the ListView control when the checked state of an item changes.
3.TextChangedThe TextChanged event is found in the ListView control when the property of the text changes.
4.ItemCheckWhen the status of a check item changes, the ItemCheck event is found in the list view control.
5.AfterLabelEditEventIt occurs when the user in the ListView control edits the label for an item.
Events and Description of ListView Control in VB.net

Let’s create a program to insert the records in the ListView control of the VB.NET Windows form.

Public Class Form1
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        ListView1.View = View.Details  ' Display the List in details  
        ListView1.GridLines = True   ' Set the Grid lines   
        ListView1.Columns.Add("Student Name", 200, HorizontalAlignment.Left) ' set the name of column  
        ListView1.Columns.Add("Address", 300, HorizontalAlignment.Left) ' set the name of column  
        ListView1.Columns.Add("Email", 100, HorizontalAlignment.Left) ' set the name of column  
        ListView1.Columns.Add("Course", 100, HorizontalAlignment.Left) ' set the name of column  
        ListView1.BackColor = Color.LightSkyBlue
    End Sub

    Private Sub Label3_Click(sender As Object, e As EventArgs) Handles Label3.Click

    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim str(4) As String
        Dim itm As ListViewItem
        str(0) = TextBox1.Text  'Accept value from the user.  
        str(1) = TextBox2.Text
        str(2) = TextBox3.Text
        str(3) = TextBox4.Text
        itm = New ListViewItem(str)
        ListView1.Items.Add(itm) 'Add the items into the ListView  
    End Sub
End Class

Program Output:

ListView Control in VB.net Output
ListView Control in VB.net Output

Now, we fill all the details of the Student that is asked in the Form.

ListView Control in VB.net Filling Up Details
ListView Control in VB.net Filling Up Details

Now, click on the Save button. It displays the record in the ListView control or in Student details table, as shown below.

ListView Control in VB.net Display Student Details
ListView Control in VB.net Display Student Details

Summary

In this article, we discussed How To Create a ListView control in Windows Forms in VB.net using Microsoft Visual Studio. After that, we saw how to use various properties, methods and Events.


Leave a Comment