Visual Basic Combo API Routines
FindWindowEx: Obtain Combo Box Edit Window Handle
     
Posted:   Thursday May 13, 1999
Updated:   Monday December 26, 2011
     
Applies to:   VB4-32, VB5, VB6, and VB3, VB4-16 with appropriate declarations
Developed with:   VB6, Windows 98
OS restrictions:   None
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
None.

This method shows how you can obtain the handle to the edit portion of a combo box using the FindWindowEx API. Unlike the GetComboBoxInfo method linked above, this method will work with all versions of Windows 95/98/NT4/2000/XP so far released.

As a further check against the handle returned, you could also add a call to GetClassName() and compare the return vale for the string "Edit" before using the handle.

 BAS Module Code
None.

 Form Code
Add a combo and a command button to a form using their default names, 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 Declare Function FindWindowEx Lib "user32" _
   Alias "FindWindowExA" _
  (ByVal hWnd1 As Long, _
   ByVal hWnd2 As Long, _
   ByVal lpsz1 As String, _
   ByVal lpsz2 As String) 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 hwndEdit As Long
   
  'get the handle to the edit portion
  'of the combo control
   hwndEdit = FindWindowEx(Combo1.hwnd, 0&, vbNullString, vbNullString)
   
  'prove you got it by changing its text (style 0 only)
   Call SetWindowText(hwndEdit, "FindWindowEx found it!")
   
  'add it to the list...
   Call SendMessage(Combo1.hwnd, CB_ADDSTRING, _
                       0&, ByVal "FindWindowEx: Edit handle is " & CStr(hwndEdit))
   
End Sub
 Comments
Searching further using FindWindowEx will not obtain the handle to the List portion of the combo, because the parent of the list is the desktop, not the combo box.

 
 

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