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.
17 comments:
hi..i'm a beginner...thats a really nice tutorial...can you make a program like this but has a database?...
thanks, this helped me out
thanks this was very helpful.
hi. ur code really helps. .
can you give codes that connects to the database?
what if i create another form for the fields that i wanted to be filled up and then the listview is on the another form what should i do?
@kram
- You can create public variables on the popup form. fill-up these variables when you hit the save or ok button to return back on the form w/ listview.
Private Sub AddItem()
Dim frmNew as New frmNewItem
frmNew.ShowDialog()
Dim lv As ListViewItem = ListView1.Items.Add(frmNew.FirstName)
lv.SubItems.Add(frmNew.LastName
)
End Sub
@ fryan
i'm still confused coz this is what i wanted to happen oK??
i create a system in vb.net its a simple student registration ok..then i have 2 forms 1st contains a listview and the ADD,EDIT,DELETE buttons...the 2nd form contains the fields that i wanted to be fill up by the user..this is want i want to happen when i click btnADD appears the 2nd form then fill up fields and then added to the 1st form which contains the listview..and of course it has a database wherein data is stored..please help i need it on my project
Please refer to my last answer. You have to create variables on the popup form and on the SAVE button of your popup form.. pass all the information on text boxes into the variables.
'POPUP FORM
Public FirstName As String
Public LastName As String
Private Sub btnSave_Click()
FirstName = txtFirstName.Text
LastName = txtLastName.Text
Me.Close
End Sub
'PARENT FORM WHERE LISTVIEW IS LOCATED
Private Sub btnAdd_Click()
Dim frmNew as New frmNewItem
frmNew.ShowDialog()
Dim lv As ListViewItem = ListView1.Items.Add(frmNew.FirstName)
lv.SubItems.Add(frmNew.LastName
)
End Sub
Hope this help
Good tutorial, thanks much.
I made it even better by adding LINQ queries, and a load event to connect it to data. Its a really simple alternative to using a DatagridView.
Simple but very useful
Simple but very useful
hehehe thnks sa code heheh very useful
Everything worked great but none of the names I add to the Listview stays there after closing the application. There doesn't seem to be any code to create a XML file to store the saved names on the listview. Help!
thanx a lot,superb coding,simple and easy to use.
can u help me with regards on SQL databse.. how can i store a listview items into the databse.. thanks alot.
how about search i need a tutorial for search and display in the listview
how about a search i want a tutorial for search and display the result in the listview
Post a Comment