Visual Basic Combo API
Obtaining the Combo Box Edit and List Handles on Win98/Win2000/WinXP
     
Posted:   Thursday May 13, 1999
Updated:   Monday December 26, 2011
     
Applies to:   VB4-32, VB5, VB6
Developed with:   VB6, Windows 98
OS restrictions:   Win98, Windows 2000, Windows XP
Author:   VBnet - Randy Birch
     

Related:  

WM_CONTEXTMENU: Kill the Default Text Box Context Menu
SendMessage: Make a Style-0 Combo Edit Read-Only

FindWindowEx: Obtain Combo Box Edit Window Handle
GetComboBoxInfo: Obtain Combo Box Edit and List Handles on Win98/NT5
       
 Prerequisites
Windows 98 or Windows 2000, Windows XP.

Win98, Windows 2000 and Windows XP provide a new API providing extended combo box information. This page shows how to use this API to fulfill very popular requests - obtaining the handle to the Edit and List portions of a VB combo box.
 BAS Module Code
None.

 Form Code
Add a combo and two command buttons to a form, along with the following code:

Option Explicit
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Copyright ©1996-2011 VBnet/Randy Birch, All Rights Reserved.
' Some pages may also contain other copyrights by the author.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Distribution: You can freely use this code in your own
'               applications, but you may not reproduce 
'               or publish this code on any web site,
'               online service, or distribute as source 
'               on any media without express permission.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Private Const CB_ADDSTRING = &H143
Private Const CB_SETITEMDATA = &H151

Private Type RECT
   Left As Long
   Top As Long
   Right As Long
   Bottom As Long
End Type

Private Type COMBOBOXINFO
   cbSize As Long
   rcItem As RECT
   rcButton As RECT
   stateButton  As Long
   hwndCombo  As Long
   hwndEdit  As Long
   hwndList As Long
End Type

Private Declare Function GetComboBoxInfo Lib "user32" _
  (ByVal hwndCombo As Long, _
   CBInfo As COMBOBOXINFO) As Long

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 SetWindowText Lib "user32" _
   Alias "SetWindowTextA" _
  (ByVal hwnd As Long, _
   ByVal lpString As String) As Long



Private Sub Command1_Click()

   Dim hEdit As Long

   hEdit = GetComboEditHandle(Combo1)
   
  'prove you got it by changing the combo text (style 0 only)
   Call SetWindowText(hEdit, "Hey! that's it .." & CStr(hEdit))
   
  'add the handle to the list...
   Call SendMessage(Combo1.hwnd, CB_ADDSTRING, _
                    0&, ByVal "GetComboBoxInfo: Edit handle is " & CStr(hEdit))
                            
End Sub


Private Sub Command2_Click()

   Dim hList As Long

   hList = GetComboListHandle(Combo1)
   
  'add the handle to the list...
   Call SendMessage(Combo1.hwnd, CB_ADDSTRING, _
                    0&, ByVal "GetComboBoxInfo: List handle is " & CStr(hList))
                       
   hwndList = hList
                            
End Sub


Private Function GetComboEditHandle(ctl As ComboBox) As Long

   Dim CBI As COMBOBOXINFO

   CBI.cbSize = Len(CBI)
   
   Call GetComboBoxInfo(ctl.hwnd, CBI)
   
   GetComboEditHandle = CBI.hwndEdit
   
End Function


Private Function GetComboListHandle(ctl As ComboBox) As Long

   Dim CBI As COMBOBOXINFO

   CBI.cbSize = Len(CBI)
   
   Call GetComboBoxInfo(ctl.hwnd, CBI)
   
   GetComboListHandle = CBI.hwndList
   
End Function
 Comments
Remember that GetComboBoxInfo is not present in Windows 95 or WinNT4. For those operating systems, you'll need to use the method detailed on the page FindWindowEx: Obtain Combo Box Edit Window Handle instead. The method there works with all operating systems tested thus far.

 
 

PayPal Link
Make payments with PayPal - it's fast, free and secure!

 
 
 
 

Copyright ©1996-2011 VBnet and Randy Birch. All Rights Reserved.
Terms of Use  |  Your Privacy

 

 

Hit Counter