Populating A ListView Quickly

Posted on 2006-06-03
Medium Priority
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 143

Assisted Solution

by:Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3] earned 100 total points
ID: 16822674
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 86

Assisted Solution

by:Mike Tomlinson
Mike Tomlinson earned 300 total points
ID: 16823252
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

JR2003 earned 600 total points
ID: 16832641
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

ID: 16846939
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

ID: 16876570
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:

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

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…
Suggested Courses
Course of the Month14 days, 7 hours left to enroll

807 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