Microsoft KB Archive/85991

= How to Programmatically Display or Hide a VB Combo Box List =

Article ID: 85991

Article Last Modified on 11/18/2003

-

APPLIES TO


 * Microsoft Visual Basic 3.0 Professional Edition
 * Microsoft Visual Basic 3.0 Professional Edition
 * Microsoft Visual Basic 1.0 Standard Edition

-



This article was previously published under Q85991



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 &quot;%{Down}&quot;



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:

 Start a new project in Visual Basic. Form1 is created by default. Place a combo box (Combo1) and a command button (Command1) on Form1.  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 &quot;User&quot; as Integer Declare Function GetParent Lib &quot;User&quot; (ByVal hWnd as Integer) as Integer Declare Function SendMessage Lib &quot;User&quot; (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

  Add the following code to the Form1 Load event procedure to put some items in the combo box: Sub Form_Load Combo1.AddItem &quot;apple&quot; Combo1.AddItem &quot;orange&quot; Combo1.AddItem &quot;banana&quot; End Sub

  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

 Press the F5 key to run the program. Click Command1 to display the list of the combo box.</ol>

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: KB85991

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.