McMillan's Visual Basic Code - Code Snippets - Fast AddItem and Clear methods for the Combo Box
Code Snippets
Home



Fast AddItem and Clear methods for the Combo Box

Using this code you can shorten the time it takes to load a Combo Box. It works best when you're adding numbers (up to 3 times faster) or short strings (up to 2 times faster). Download the sample project to see for yourself.

Note: LB_ADDSTRING can be used in same way to add items to a List Box. But in the case of the List Box there is no speed increase.

Option Explicit

Private Const CB_ERR As Long = -1
Private Const CB_ADDSTRING As Long = &H143
Private Const CB_RESETCONTENT As Long = &H14B
Private Const CB_SETITEMDATA As Long = &H151

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

Public Sub AddItem(cmb As ComboBox, Text As Variant, Optional ItemData As Long)

   Dim l As Long
   Dim s As String

   If VarType(Text) = vbString Then
      s = Text
   Else
      s = Trim$(Str$(Text))
   End If

   l = SendMessage(cmb.hwnd, CB_ADDSTRING, 0&, ByVal s)
   If l <> CB_ERR Then
      SendMessage cmb.hwnd, CB_SETITEMDATA, l, ByVal ItemData
   End If

End Sub

Public Sub Clear(cmb As ComboBox)

   SendMessage cmb.hwnd, CB_RESETCONTENT, 0, 0&

End Sub

  FastCombo.zip - contains: Fast Combo Sample Test (7 kb)


© Copyright Notice

Unless otherwise stated, the code on this site is Copyright to Andrew McMillan. You may use this code in your projects (both commercial and non-commercial) but you are not permitted to republish this code in any form without the Author's prior consent.

The code on this site is supplied "as is" and no claims are made as to its soundness. The Author claims no responsibility for or liability from use of said source code.


Home