Prevent a window from repainting

This tip show show to prevent an area of a window from repainting. When you have a lot of items to add to a control, such as a ListBox or ListView, this can considerably speed up the process. On my system, it speeds up adding 10,000 items to a List Box by over 30%.

Create a new project, and add a ListBox, a Command Button and a CheckBox to the form. Change the caption of the CheckBox to '&Lock Update' and change the caption of the Command Button to '&Load...'. Then paste the following code into the form:

Private Declare Function LockWindowUpdate Lib "user32" _
    (ByVal hwndLock As Long) As Long 
Private Declare Function timeGetTime Lib "winmm.dll" () As Long 

Private Sub Command1_Click() 
Dim i As Long 
Dim lTIme As Long 

    lTIme = timeGetTime() 
    
    If (Check1.Value = Checked) Then 
        LockWindowUpdate List1.hWnd 
    End If 
    
    List1.Clear 
    For i = 1 To 10000 
        List1.AddItem "Test " & i 
    Next i 
    
    If (Check1.Value = Checked) Then 
        LockWindowUpdate 0 
        List1.Refresh 
    End If 
    
    MsgBox "Time: " & timeGetTime - lTIme 
    
End Sub 

When you click on the command button, the code will add 10,000 items to the List Box. If the Lock Update check box is checked, windows will prevent any redrawing of the List Box whilst the items are added.