Difference between revisions of "ListView Control VB6"

From Free Knowledge Base- The DUCK Project: information for everyone
Jump to: navigation, search
m
 
(One intermediate revision by one user not shown)
Line 90: Line 90:
 
  intID = frmStaffList.lv.SelectedItem
 
  intID = frmStaffList.lv.SelectedItem
 
If the list contains no items, an error is returned.
 
If the list contains no items, an error is returned.
 
+
Recommended fix: 
 
+
If lv.ListItems.Count > 0 Then
 
+
  intID = frmStaffList.lv.SelectedItem
  
 
 
 
 

Latest revision as of 07:00, 2 February 2008

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