Faster items adding in ComboBox

The following code snippet adds 5000 items into a ComboBox. It does it in 2 ways: with the standard AddItem method that Visual Basic provides, and by using Win32 API.
As you'll see, adding ComboBox items with Win32 API is much faster than doing it with standard VB way...


'Example for adding items into combo by using Win32 API.
'
'Written by Nir Sofer
'Web site: http://nirsoft.mirrorz.com

Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" _
(ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Private Const CB_ADDSTRING = &H143


Private Sub cmdComboAPI_Click()
    Dim strItemText     As String
    Dim intIndex        As Integer
    Dim dblTimer        As Double
    
    cmbTest.Clear
    dblTimer = Timer
    'Adding items with Win32 API
    For intIndex = 1 To 5000
        strItemText = "item number " & CStr(intIndex)
        SendMessage cmbTest.hWnd, CB_ADDSTRING, 0, ByVal strItemText
    Next
    MsgBox Format(Timer - dblTimer, "0.000") & " seconds"

End Sub

Private Sub cmdComboVB_Click()
    Dim strItemText     As String
    Dim intIndex        As Integer
    Dim dblTimer        As Double
    
    cmbTest.Clear
    dblTimer = Timer
    'Adding items with standard AddItem method.
    For intIndex = 1 To 5000
        strItemText = "item number " & CStr(intIndex)
        cmbTest.AddItem strItemText
    Next
    MsgBox Format(Timer - dblTimer, "0.000") & " seconds"

End Sub

Download this sample project