What is ListView Control in VB.net?
The ListView Control in VB.net is used to show a group of items in Windows Forms.
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.
Let’s create a ListView in VB.net by dragging a ListView Control from the Toolbox and dropping it on the form.
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 Properties | Description |
---|---|---|
1. | Alignment | Gets or sets the alignment of items in the control. |
2. | AutoArrange | Gets or sets whether icons are automatically kept arranged. |
3. | BackColor | Gets or sets the background color. |
4. | CheckBoxes | Gets or sets a value indicating whether a check box appears next to each item in the control. |
5. | CheckedIndices | Gets the indexes of the currently checked items in the control. |
6. | CheckedItems | Gets the currently checked items in the control. |
7. | Columns | Gets the collection of all column headers that appear in the control. |
8. | GridLines | Gets or sets a value indicating whether grid lines appear between the rows and columns containing the items and subitems in the control. |
9. | HeaderStyle | Gets or sets the column header style. |
10. | HideSelection | Gets or sets a value indicating whether the selected item in the control remains highlighted when the control loses focus. |
11. | HotTracking | Gets 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. | HoverSelection | Gets or sets a value indicating whether an item is automatically selected when the mouse pointer remains over the item for a few seconds. |
13. | InsertionMark | Gets an object used to indicate the expected drop location when an item is dragged within a ListView control. |
14. | Items | Gets a collection containing all items in the control. |
15. | LabelWrap | Gets or sets a value indicating whether item labels wrap when items are displayed in the control as icons. |
16. | LargeImageList | Gets or sets the ImageList to use when displaying items as large icons in the control. |
17. | MultiSelect | Gets or sets a value indicating whether multiple items can be selected. |
18. | RightToLeftLayout | Gets or sets a value indicating whether the control is laid out from right to left. |
19. | Scrollable | Gets 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. | SelectedIndices | Gets the indexes of the selected items in the control. |
21. | SelectedItems | Gets the items that are selected in the control. |
22. | ShowGroups | Gets or sets a value indicating whether items are displayed in groups. |
23. | ShowItemToolTips | Gets or sets a value indicating whether ToolTips are shown for the ListViewItem objects contained in theListView. |
24. | SmallImageList | Gets or sets the ImageList to use when displaying items as small icons in the control. |
25. | Sorting | Gets or sets the sort order for items in the control. |
26. | StateImageList | Gets or sets the ImageList associated with application-defined states in the control. |
27. | TopItem | Gets or sets the first visible item in the control. |
28. | View | Gets 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. | VirtualListSize | Gets or sets the number of ListViewItem objects contained in the list when in virtual mode. |
30. | VirtualMode | Gets or sets a value indicating whether you have provided your own data-management operations for the ListView control. |
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 Methods | Description |
---|---|---|
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. |
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 Events | Description |
---|---|---|
1. | ItemActivate | The ItemActivate event occurred when an item activated in the ListView control. |
2. | ItemChecked | The ItemChecked event is found in the ListView control when the checked state of an item changes. |
3. | TextChanged | The TextChanged event is found in the ListView control when the property of the text changes. |
4. | ItemCheck | When the status of a check item changes, the ItemCheck event is found in the list view control. |
5. | AfterLabelEditEvent | It occurs when the user in the ListView control edits the label for an item. |
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:
Now, we fill in all the details of the Student that is asked in the Form.
Now, click on the Save button. It displays the record in the ListView control or in the Student details table, as shown below.
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.
PREVIOUS
NEXT