How to add a Listview control as runtime

Posted on 2004-11-22
Medium Priority
Last Modified: 2008-02-01
How do I add a ListView control to a form at runtime and add 10 columns.
Question by:rdwillett
1 Comment
LVL 55

Accepted Solution

Ryan Chong earned 400 total points
ID: 12651456
Try customize the below example:

Dim WithEvents lv As MSComctlLib.ListView

Private Sub Form_Load()
    Me.ScaleMode = vbPixels
    Set lv = Controls.Add("MSComctlLib.ListViewCtrl.2", "lv")
    lv.Visible = True
    lv.Left = 0
    lv.Top = 0
    lv.Width = Me.ScaleWidth
    lv.Height = Me.ScaleHeight
    lv.View = lvwReport
    lv.FullRowSelect = True
    For i = 1 To 10
        lv.ColumnHeaders.Add i, , "Column " & i
    Next i
    For i = 1 To 5
        lv.ListItems.Add i, , "Row " & i
        For j = 1 To 9
            lv.ListItems(i).SubItems(j) = "Subitem " & j & " for Row " & i
        Next j
    Next i
End Sub

Private Sub lv_ItemClick(ByVal Item As MSComctlLib.ListItem)
    MsgBox "You clicking item " & Item.Text
End Sub

Hope this helps

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses
Course of the Month8 days, 19 hours left to enroll

621 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question