VB icon

A ListView With Headers Example

Submitted on: 1/26/2015 9:03:00 AM
By: Christopher Hemple (from psc cd)  
Level: Beginner
User Rating: By 22 Users
Compatibility: VB 5.0, VB 6.0
Views: 2864
     This Code will Show you how to use list Headers
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
' Name: A ListView With Headers Example
' Description:This Code will Show you how to use list Headers
' By: Christopher Hemple (from psc cd)
' Assumes:U Will need 3 command buttons with the default names, and 1 listview with the default name.
Put these captions for the Command Buttons
Command1 = Add
Command2 = Delete
Command3 = Exit

Private Sub Command1_Click()
'Inputbox Code
Dim name 'Remember name
Dim age 'Remember age
Dim dob 'remember dob
name = InputBox("What is the persons name ?", "Name ?") 'Show a inputbox for the persons name
age = InputBox("What is the personsage ?", "Age ?") 'Show a inputbox for the persons age
dob = InputBox("What is the persons Date Of Birth ?", "Date Of Birth ?") 'Show a inputbox for the persons Date Of Birth
'End Of Inputbox Code
'Adding To List Code
Dim ListObj As ListItem 'Set listObj as a listitem
Set ListObj = ListView1.ListItems.Add(, , name) 'this allways adds to the 1st column , this lines adds the name to the 1st Column
ListObj.SubItems(1) = age 'this allways adds to the second column , this lines adds the age to the 2nd Column
ListObj.SubItems(2) = dob 'this allways adds to the third column , this lines adds the dob to theb 3rd Column
'End Of Adding To List Code
End Sub
Private Sub Command2_Click()
On Error Resume Next 'If Theres a error resume the next line ( the error here would be nothing in the listview or no selected item )
ListView1.ListItems.Remove ListView1.SelectedItem.Index 'Delete The Selected Item
End Sub
Private Sub Command3_Click()
Unload Me 'Exits The Program
End Sub
Private Sub Form_Load()
'This Code is needed
ListView1.View = lvwReport 'Set The Listview1 View So we Can See Our Columns/Headers
ListView1.ColumnHeaders.Add , , "Name" 'Add a column Called Name
ListView1.ColumnHeaders.Add , , "Age" 'Add a column Called Age
ListView1.ColumnHeaders.Add , , "Date Of Birth" 'Add a column Called Age
'End Of Needed Code
End Sub
Private Sub Form_Unload(Cancel As Integer)
'My Code
If MsgBox("If This Code Helped You Please Come Back and Either Vote Or Comment,:). Would You Like To Vote Or Comment Now?", vbYesNo, "Thankx For Using My Code") = vbYes Then GoTo open_url
Exit Sub
MsgBox "I have Copyed The Url To The clipboard", vbInformation, "Url"
Clipboard.SetText "http://www.pscode.com/vb/scripts/ShowCode.asp?txtCodeId=36543&lngWId=1"
End Sub
Private Sub ListView1_DblClick()
On Error Resume Next ' resume The Next line On a Error
MsgBox "Name : " + ListView1.SelectedItem.Text + vbCrLf + "Age : " + ListView1.SelectedItem.SubItems(1) + vbCrLf + "Dob : " + ListView1.SelectedItem.SubItems(2) 'Make The Msgbox
End Sub

Other 5 submission(s) by this author


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Beginner category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)

Other User Comments

 There are no comments on this submission.

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)

To post feedback, first please login.