ListView Control VB6

Revision as of 20:15, 21 January 2008 by Admin (Talk | contribs)

ListView: Microsoft Windows Common Controls 5.0 (SP2)

Add the Component "Microsoft Windows Common Controls 5.0 (SP2)" by right-clicking on the Toolbox and selecting "Components", then the "controls" tab.

Using ListView

Create a ListView called "lvCustomDraw"

The "View" Property controls the appearance of the ListView. Try 3 - lvwReport.

 Private Sub Form_Load()
   ' fill the listview with some stuff....
   With lvCustomDraw
     .ColumnHeaders.Add , , "Item Column"
     .ColumnHeaders.Add , , "Subitem 1"
     .ColumnHeaders.Add , , "Subitem 2"
     
     Dim i&
 
     For i = 0 To 99
       With .ListItems.Add(, , "Item " & CStr(i))
         .SubItems(1) = "Subitem 1"
         .SubItems(2) = "Subitem 2"
       End With
     Next
   End With                     
 End Sub
 

Basic Example

 Private Sub Form_Load()
   lvBox.View = lvwReport
   lvBox.ColumnHeaders.Add , , "First Name", lvBox.Width / 3
   lvBox.ListItems.Add , , "Lisa"
   lvBox.ListItems.Add , , "Savannah"
   lvBox.ListItems.Add , , "Kathy"
   lvBox.ListItems.Add , , "Sara"
   lvBox.ListItems.Add , , "Jessica"
   lvBox.ListItems.Add , , "Lindsey"
   lvBox.ListItems.Add , , "Kara"
   lvBox.ListItems.Add , , "Kristal"
   lvBox.ListItems.Add , , "Stacy"
 End Sub

Update ListView Example

 Option Explicit
 Dim i As Integer
 Dim conn As ADODB.Connection, rec As ADODB.Recordset, fld As ADODB.Field
 Dim li As ListItem, lj As ListItem
 
 Private Sub Command1_Click()
   rec.AddNew
   rec.Fields(0) = "bird"
   rec.Fields(1) = 10
   rec.Fields(2) = 20
   If Not rec.EOF Then rec.MoveNext
   L1.ListItems.Clear
   Call loadlistview(L1, rec)
 End Sub
 
 Private Sub Form_Load()
   Set conn = New ADODB.Connection
   Set rec = New ADODB.Recordset
   conn.Open ("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\My Documents\Test_1.mdb;Persist Security Info=False")
   rec.Open "testable", conn, adOpenDynamic, adLockOptimistic
 End Sub
  
 Sub loadlistview(L1 As ListView, rec As ADODB.Recordset)
 
 Set L1.Icons = ImageList2
   rec.MoveFirst
   Do Until rec.EOF
   Set li = L1.ListItems.Add(, , rec.Fields(0) & "", , 3)
   rec.MoveNext
   Loop
   L1.ColumnHeaders.Add , , rec.Fields(0).Name
 End Sub
 
 Private Sub Form_Unload(Cancel As Integer)
   rec.Close
   conn.Close
   Set conn = Nothing
 End Sub

Object variable or With variable not set

Consider the line of code:

intID = frmStaffList.lv.SelectedItem

If the list contains no items, an error is returned. Recommended fix:

If lv.ListItems.Count > 0 Then
  intID = frmStaffList.lv.SelectedItem

 

 

 

Last modified on 21 January 2008, at 20:15