How to Add Items At Runtime
For the sample code to run you will need to do the following
things:
1. Add from the components list Microsoft Windows Common
controls 5.0(Vb5) or 6.0(Vb6).
2. Add Listview control,2 textboxes and a command button to
your form.
Option Explicit
Dim i As Integer
Dim lsitem As ListItem
Private Sub Command1_Click()
'change text1 to change the the item in the
first column.
Set lsitem = Listview1.ListItems.Add(, Text1.Text,
Text1.Text)
'change text2 to change the 2nd
column
lsitem.SubItems(1) = Text2.Text
End Sub
Private Sub Form_Load()
'this change the view to
report view
Listview1.View = lvwReport
'this adds the column headers to the
listview
'Syntax for the colmn headers add method
'Add(index, key, text, width, alignment)
Listview1.ColumnHeaders.Add 1, "col1", "Column1"
Listview1.ColumnHeaders.Add 2, "col2", "Column2"
For i = 1 To 5
' Syntax for the listitems add method
'Add(index, key, text, icon, smallIcon)
Set lsitem = Listview1.ListItems.Add(, "a" & i, "Item"
& i)
'For the subitems to show up the list
view has to be in report view 'and the list view has to
have Column Headers
lsitem.SubItems(1) = "subitem"
Next
End Sub