How to Programmatically Display or Hide a VB Combo Box List

ID: Q85991


The information in this article applies to:
  • Microsoft Visual Basic Standard and Professional Editions for Windows, version 3.0
  • Microsoft Visual Basic programming system for Windows, version 1.0


SUMMARY

The list of a Visual Basic drop-down combo box (Style=0 or 2) is usually opened and closed by using a mouse. However, you can also open and close the list of a combo box programmatically by using the Windows SendMessage function as described below.

However, there is an easy way. In visual basic, when a drop-down combo box has the focus, you can press ALT-DOWN to open it up. Therefore, you can use SendKeys to send these keys to the combo box, as in this example:

To do this programmatically, use the following code to change focus to the combo box and send the necessary keystroke:


   combo1.SetFocus
   SendKeys "%{Down}" 


MORE INFORMATION

The CB_SHOWDROPDOWN constant can be used with the SendMessage function to programmatically open or close the list of a Visual Basic drop-down combo box of Style=0 or Style=2 (Style=1 always has the list displayed). The following steps demonstrate how to open the list of a drop-down combo box:

  1. Start a new project in Visual Basic. Form1 is created by default.


  2. Place a combo box (Combo1) and a command button (Command1) on Form1.


  3. Add the following declarations and constants to the general Declarations section of Form1:
    
       ' Enter each Declare statement as one, single line:
       Declare Function GetFocus Lib "User" () as Integer
       Declare Function GetParent Lib "User" (ByVal hWnd as Integer)
          as Integer
       Declare Function SendMessage Lib "User" (ByVal hWnd as Integer,
          ByVal wMsg as Integer, ByVal wParam as Integer,
          ByVal lParam as Any) as Long
       Global Const WM_USER = &H400
       Global Const CB_SHOWDROPDOWN = WM_USER + 15
     


  4. Add the following code to the Form1 Load event procedure to put some items in the combo box:
    
       Sub Form_Load ()
          Combo1.AddItem "apple"
          Combo1.AddItem "orange"
          Combo1.AddItem "banana"
       End Sub
     


  5. Add the following code to the Command1_Click event procedure:
    
       Sub Command1_Click ()
          Combo1.SetFocus
          cbhWnd% = GetFocus ()
          cblisthWnd% = GetParent (cbhWnd%)
          cbFunc% = -1           'cbFunc% = -1 displays the list
                                 'cbFunc% = 0 hide the list
          retval& = SendMessage (cblisthWnd%, CB_SHOWDROPDOWN, cbFunc%, 0&)
       End Sub
     


  6. Press the F5 key to run the program. Click Command1 to display the list of the combo box.


If Style=2 for the combo box, there is no need to use the GetParent function. Use the return value of the GetFocus (cbhWnd% in the above example) call as the first parameter of the SendMessage function.

NOTE: The list of a combo box with Style=0 or 2 will close when the combo box loses focus.

Additional query words: 2.00 3.00

Keywords :
Version :
Platform :
Issue type :


Last Reviewed: June 8, 1999
© 2000 Microsoft Corporation. All rights reserved. Terms of Use.