At the end of the tutorial, you will be able to do the following tasks:
1. Properly configure ListView to display spread-sheet information
2. Add Items on ListView
3. Edit Item on ListView
4. Delete Item on ListView
Here are the step-by-step to create our list view tutorial project.
1. Create a new project in VB.NET
2. Put the following controls as shown below on your initial form

3. Set the ListView control and set the following properties from the Property window.
a. FullRowSelect = True
b. GridLines = True
c. HideSelection = False
d. MultiSelect = False
e. View = Details
4. Declare blnAdd variable that will indicate if Add button was pressed.
Dim blnAdd As Boolean 'indicator if add button was pressed
5. On your add button, put the following codes:
Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
If btnAdd.Text.ToLower() = "add" Then 'if caption is ADD then perform Add process
txtFirstName.Enabled = True
txtLastName.Enabled = True
btnAdd.Text = "Save"
btnEdit.Text = "Cancel"
btnDelete.Enabled = False
txtFirstName.Text = ""
txtLastName.Text = ""
blnAdd = True
Else 'save process
txtFirstName.Enabled = False
txtLastName.Enabled = False
btnAdd.Text = "Add"
btnEdit.Text = "Edit"
btnDelete.Enabled = True
If blnAdd Then
AddItemToListView()
Else
EditItemInListView()
End If
End If
End Sub
Also include these codes below the btnAdd_Click, these functions are for adding and editing listview items.
'''
''' Add listview items
'''
'''
Private Sub AddItemToListView()
'Usually the first unique colum is the root item
Dim lv As ListViewItem = ListView1.Items.Add(txtFirstName.Text)
'The remaining columns are subitems
lv.SubItems.Add(txtLastName.Text)
End Sub
'''
''' Edit Item in ListView
'''
'''
Private Sub EditItemInListView()
If ListView1.SelectedItems.Count > 0 Then 'make sure there is a selected item to modify
ListView1.SelectedItems(0).Text = txtFirstName.Text
ListView1.SelectedItems(0).SubItems(1).Text = txtLastName.Text
End If
End Sub
6. On the edit button, place the following codes.
Private Sub btnEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEdit.Click
If txtFirstName.Text.Length > 0 Then
If btnEdit.Text.ToLower() = "edit" Then 'if caption is EDIT then perform EDIT process
txtFirstName.Enabled = True
txtLastName.Enabled = True
btnAdd.Text = "Save"
btnEdit.Text = "Cancel"
btnDelete.Enabled = False
blnAdd = False
Else 'cancel process
txtFirstName.Enabled = False
txtLastName.Enabled = False
btnAdd.Text = "Add"
btnEdit.Text = "Edit"
btnDelete.Enabled = True
End If
Else
MessageBox.Show("Please select record to edit")
End If
End Sub
7. While on the delete button, copy-and-paste these codes:
Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
If ListView1.SelectedItems.Count > 0 AndAlso MessageBox.Show("Do you want to delete this item?", "Confirm", MessageBoxButtons.YesNo) = Windows.Forms.DialogResult.Yes Then 'make sure there is a selected item to delete
ListView1.SelectedItems(0).Remove()
End If
End Sub
8. To select an item and put the values on the respective textboxes, use the following codes:
Private Sub ListView1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListView1.SelectedIndexChanged
If ListView1.SelectedItems.Count > 0 Then
txtFirstName.Text = ListView1.SelectedItems(0).Text
txtLastName.Text = ListView1.SelectedItems(0).SubItems(1).Text
End If
End Sub
To see and examine the complete project, you can download it here.
To stay up-to-date on Technology news, subscribe now.