Populating A ListView Quickly

Posted on 2006-06-03
Last Modified: 2013-12-25
I have a listview that I populate with a list of files in a directory specified by the user. One particular directory that users often refer to contains 4000+ files, but it takes forever to populate the listview (using ListItems.add).

When I view that same directory using Explorer, the entire list of files appears more or less instantly, yet Explorer is also a listview. How do I get to populate my listview as fast as Explorer does?

Any help is much appreciated.
Question by:TimHudspith
    LVL 142

    Assisted Solution

    by:Guy Hengel [angelIII / a3]
    tip 1:
    * hide the listview while adding the items to id
    tip 2:
    don't load so many files at once into a listview

    ps: the Explorer is NOT a simple listview, and loads NOT all files at once, only the first screenful, and then continues to load in the background.
    LVL 85

    Assisted Solution

    by:Mike Tomlinson
    Suppress the updating of your ListView while you add items via the WM_SETREDRAW msg:

    Option Explicit

    Private Const WM_SETREDRAW = &HB

    Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long

    Private Sub Command1_Click()
        SendMessage ListView1.hwnd, WM_SETREDRAW, False, 0 ' Turn OFF updates
        ' Add all of your ListView items in here...
        SendMessage ListView1.hwnd, WM_SETREDRAW, True, 0 ' Turn ON updates
    End Sub
    LVL 18

    Accepted Solution

    Can you paste in the code you use to populate the list view?
    There might be some optimizations we can make to improve the performance.

    You might get it to load a bit faster if you use a with block to reduce the number of dots in a  line e.g.:

    Option Explicit

    Private Sub Dir1_Change()

        On Error Resume Next
        Dim fso As Scripting.FileSystemObject
        Set fso = New Scripting.FileSystemObject
        'Dim fso As Object
        'Set fso = CreateObject("Scripting.FileSystemObject")
        'Dim MyFolder As Object
        Dim MyFolder As Scripting.Folder
        'Dim MyFile As Object
        Dim MyFile As Scripting.File
        Set MyFolder = fso.GetFolder(Dir1.Path)

        Dim itmX As ListItem
        Dim subItmX As ListSubItem
        Label1.Caption = MyFolder.Files.Count & " files"
        With ListView1
            With .ListItems
                For Each MyFile In MyFolder.Files
                    Set itmX = .Add(, , MyFile.Name)
                    With itmX.ListSubItems
                        Set subItmX = .Add(, , MyFile.Size)
                        Set subItmX = .Add(, , MyFile.DateCreated)
                        Set subItmX = .Add(, , MyFile.DateLastAccessed)
                        Set subItmX = .Add(, , MyFile.DateLastModified)
                        Set subItmX = .Add(, , MyFile.ShortName)
                        Set subItmX = .Add(, , MyFile.Type)
                    End With
                Next MyFile
                Set MyFile = Nothing
            End With
        End With

    End Sub

    Private Sub Drive1_Change()
        Dir1.Path = Drive1.Drive
    End Sub

    Private Sub Form_Load()
        With ListView1
            With .ColumnHeaders
                .Add , , "Name"
                .Add , , "Size"
                .Add , , "Create Date"
                .Add , , "Last Accessed"
                .Add , , "Modified"
                .Add , , "Short Name"
                .Add , , "Type"
            End With
        End With
    End Sub

    Author Comment

    IdleMind & AngelIII : thanks for those tips but they didn't make any appreciable difference. Thanks anyway.

    Likewise JR2003 - no real difference but I used your code as it was a lot cleaner than mine.

    I think I'll have to rethink the whole thing. Now I think of it, listing 4000 files in a listview isn't much use to anyone.

    LVL 18

    Expert Comment

    If it's just the file-name you want to show in the list you could use VB's FileList control or just a standard listbox instead which are much faster controls than the ListView control.

    If you have some money to spend you could try an off the shelf product like filleview.
    There as a sample you can download here:

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to improve team productivity

    Quip adds documents, spreadsheets, and tasklists to your Slack experience
    - Elevate ideas to Quip docs
    - Share Quip docs in Slack
    - Get notified of changes to your docs
    - Available on iOS/Android/Desktop/Web
    - Online/Offline

    Suggested Solutions

    Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
    When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
    Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
    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…

    759 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

    9 Experts available now in Live!

    Get 1:1 Help Now