Working with Forms and Controls in VB.NET ListView Control
ListView in VB.NET is a powerful control that enables you to display a collection of items with different views (e.g., icon, list, detail). You can easily customize the ListView control to fit your specific needs by working with its properties, events, and methods.
Syntax
' Adding Items to a ListView Control
Dim item As ListViewItem
item = New ListViewItem("Item 1")
lstView.Items.Add(item)
' Editing Items in a ListView Control
lstView.Items(0).SubItems(1).Text = "New Value"
' Deleting Items from a ListView Control
lstView.Items.RemoveAt(0)
' Clearing All Items from a ListView Control
lstView.Items.Clear()
Example
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
' Add Items to a ListView control
Dim item As ListViewItem
item = New ListViewItem("Item 1")
item.SubItems.Add("Value 1")
lstView.Items.Add(item)
item = New ListViewItem("Item 2")
item.SubItems.Add("Value 2")
lstView.Items.Add(item)
item = New ListViewItem("Item 3")
item.SubItems.Add("Value 3")
lstView.Items.Add(item)
End Sub
Private Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click
' Add an Item to a ListView Control
Dim item As ListViewItem
item = New ListViewItem(txtItem.Text)
item.SubItems.Add(txtValue.Text)
lstView.Items.Add(item)
txtItem.Clear()
txtValue.Clear()
End Sub
Private Sub btnEdit_Click(sender As Object, e As EventArgs) Handles btnEdit.Click
' Edit an Item in a ListView Control
If lstView.SelectedItems.Count > 0 Then
lstView.SelectedItems(0).SubItems(1).Text = txtEditValue.Text
End If
End Sub
Private Sub btnDelete_Click(sender As Object, e As EventArgs) Handles btnDelete.Click
' Delete an Item from a ListView Control
If lstView.SelectedItems.Count > 0 Then
lstView.Items.RemoveAt(lstView.SelectedIndices(0))
End If
End Sub
Output
Explanation
In the above example, we have created a form with a ListView control and three buttons - Add, Edit, and Delete. When the form loads, three items are added to the ListView control.
When the user clicks the Add button, a new item is added to the ListView control with the values entered in two text boxes. When the user clicks the Edit button, the value of the selected item's second subitem is changed to the value entered in a text box. When the user clicks the Delete button, the selected item is deleted from the ListView control.
Use
The ListView control can be used to display and manipulate data in a variety of ways. It can be particularly useful when displaying large amounts of data that need to be sorted or filtered in different ways.
Important Points
- The ListView control can display items in different views (e.g., icon, list, detail).
- Items can be added to, edited in, and deleted from the ListView control.
- The ListView control can be customized using its properties, events, and methods.
Summary
In summary, the ListView control in VB.NET is a powerful tool for displaying and manipulating data in different views. It provides a range of options for customizing its appearance and behavior, and can be easily integrated into a wide variety of VB.NET applications.