How to add a Listview control as runtime

Posted on 2004-11-22
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 48

    Accepted Solution

    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

    Better Security Awareness With Threat Intelligence

    See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

    Join & Write a Comment

    Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
    When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
    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…
    Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

    755 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

    Need Help in Real-Time?

    Connect with top rated Experts

    26 Experts available now in Live!

    Get 1:1 Help Now