Microsoft KB Archive/124057

= How to Force a DropDown and Retract of the List in a Combo Box =

Article ID: 124057

Article Last Modified on 12/9/2003

-

APPLIES TO


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

-



This article was previously published under Q124057



SUMMARY
This article shows you how to send a message to the Combo box to force the List portion to drop or retract at any time.

Usually, to drop the List portion of a Combo box, you click the dropdown button located to the right of the Combo's edit box, or you can press the F4 key when the control has the focus. To have the Combo box retract the List portion, you click the dropdown button again, use the mouse or keyboard to choose an item from the list, press the ESC or F4 key, or click anywhere else on the screen.



MORE INFORMATION
To send the CB_SHOWDROPDOWN message to a Combo box of Style 0 - Dropdown Combo or 2 - Dropdown List, call the SendMessage API function. If the wParam parameter is any non-zero integer, the List will drop. If wParam equals 0, the List will retract. Note that a Combo box of Style 1 (Simple Combo) always has its list visible.

Step-by-Step Example
 Start a new project in Visual Basic (ALT, F, N). Form1 is created by default.  Add the following statements to the General Declarations section of Form1: ' Enter the following two lines as one, single line:

Declare Function SendMessage Lib "User" (ByVal hWnd As Integer,     ByVal wMsg As Integer, ByVal wParam As Integer, lParam As Any) As Long

‘For Windows 95 and 98 Const WM_USER = &H400 Const CB_SHOWDROPDOWN = (WM_USER + 15)

‘For Windows NT, 2000 and XP, define CB_SHOWDROPDOWN as below ‘Const CB_SHOWDROPDOWN = &H14F

  Add the following code to the Form's Load event: Sub Form_Load combo1.AddItem "item# 1" combo1.AddItem "item# 2" combo1.AddItem "item# 3" combo1.AddItem "item# 4" End Sub

 Place a Combo box (Combo1) on Form1. Place a Command button (Command1) on Form1, and set its Caption property to DropDown.  Add the following code to the Command1 button's Click event: Sub Command1_Click x% = SendMessage(combo1.hWnd, CB_SHOWDROPDOWN, 1, 0&) End Sub

 Place a second Command button (Command2) on Form1, and set its Caption property to Retract.</li>  Add the following code to the Command2 button's Click event: Sub Command2_Click x% = SendMessage(combo1.hWnd, CB_SHOWDROPDOWN, 0, 0&) End Sub

</li> Press the F5 key to run the program. Click the DropDown button to show the Combo box list, and click the Retract button to remove it.</li></ol>