Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
 
MSDN Visual Basic Community
 
 
 
 
TitleChange controls faster by preventing window updates
DescriptionThis example shows how to change controls faster by preventing window updates in Visual Basic 6. It uses the LockWindowUpdate API function to prevent Windows from updating while it makes the changes.
KeywordsLockWindowUpdate, modify control, change control
CategoriesControls, Windows, Tips and Tricks
 
Thanks to David Jacquet.

If you check the Lock Updates box, the program calls the LockWindowUpdates API function. It then loads a bunch of values into a ListBox, unlocks updates if it previously locked them, and displays the elapsed time.

 
Private Sub Command1_Click()
Dim start_time As Single
Dim i As Long
Dim num_items As Long

    num_items = CLng(txtNumItems.Text)
    List1.Clear
    lblTime.Caption = ""
    MousePointer = vbHourglass
    DoEvents

    start_time = Timer

    If Check1.Value = vbChecked Then
        ' Lock the window to prevent updates.
        LockWindowUpdate List1.hWnd 'locks the refresh
            ' control
    End If

    ' Create the list items.
    For i = 1 To num_items
        List1.AddItem "Item " & Format$(i) & _
            " of " & Format$(num_items)
    Next i

    If Check1.Value = vbChecked Then
        ' Unlock the window so it can update again.
        LockWindowUpdate 0
    End If

    lblTime.Caption = Format$(Timer - start_time, "0.00") & _
        _
        " seconds"

    MousePointer = vbDefault
End Sub
 
In one test, the program took 4.99 seconds to load 30,000 list items without locking. It took only 3.4 seconds with LockWindowUpdate.
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated