Login | Register   
RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX

By submitting your information, you agree that devx.com may send you DevX offers via email, phone and text message, as well as email offers about other products and services that DevX believes may be of interest to you. DevX will process your information in accordance with the Quinstreet Privacy Policy.

Tip of the Day
Language: VB4/32,VB5,VB6
Expertise: Intermediate
Nov 4, 2000



Application Security Testing: An Integral Part of DevOps

DuplicateComboBox - Fast copy of the contents of a ComboBox control

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 Declare Function LockWindowUpdate Lib "user32" (ByVal hwndLock As Long) _
    As Long
Const CB_GETCOUNT = &H146
Const CB_GETLBTEXT = &H148
Const CB_ADDSTRING = &H143

' Duplicate the contents of a ComboBox control to another ComboBox control
' Pass False to the last argument to append contents
' (the target control isn't cleared before adding elements)
' uses API functions for the fastest code

Sub DuplicateComboBox(Source As ComboBox, Target As ComboBox, _
    Optional AppendMode As Boolean)
    Dim index As Long
    Dim itmData As Long
    Dim numItems As Long
    Dim sItemText As String
    ' prepare the receiving buffer
    sItemText = Space$(512)
    ' temporarily prevent updating
    LockWindowUpdate Target.hWnd
    ' reset target contents, if not in append mode
    If Not AppendMode Then
        SendMessage Target.hWnd, CB_RESETCONTENT, 0, ByVal 0&
    End If
    ' get the number of items in the source control
    numItems = SendMessage(Source.hWnd, CB_GETCOUNT, 0&, ByVal 0&)
    For index = 0 To numItems - 1
        ' get the item text
        SendMessage Source.hWnd, CB_GETLBTEXT, index, ByVal sItemText
        ' get the item data
        itmData = SendMessage(Source.hWnd, CB_GETITEMDATA, index, ByVal 0&)
        ' add the item text to the target list
        SendMessage Target.hWnd, CB_ADDSTRING, 0&, ByVal sItemText
        ' add the item data to the target list
        SendMessage Target.hWnd, CB_SETITEMDATA, index, ByVal itmData
    ' allow redrawing
    LockWindowUpdate 0
End Sub
Francesco Balena
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.
Thanks for your registration, follow us on our social networks to keep up-to-date