Populating A ListView Quickly

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.
TimHudspithAsked:
Who is Participating?
 
JR2003Commented:
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"
    Label1.Refresh
    With ListView1
        With .ListItems
            .Clear
            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
0
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
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.
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
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
   
    ListView1.Refresh
End Sub
0
 
TimHudspithAuthor Commented:
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.

Cheers.
0
 
JR2003Commented:
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:
http://www.mabry.com/fileviewx/index.htm
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.