How to Force a DropDown and Retract of the List in a Combo Box (124057)



The information in this article applies to:

  • Microsoft Visual Basic Standard Edition for Windows 2.0
  • Microsoft Visual Basic Standard Edition for Windows 3.0
  • Microsoft Visual Basic Professional Edition for Windows 2.0
  • Microsoft Visual Basic Professional Edition for Windows 3.0

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

  1. Start a new project in Visual Basic (ALT, F, N). Form1 is created by default.
  2. 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 
    
    						
  3. 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
    
    						
  4. Place a Combo box (Combo1) on Form1.
  5. Place a Command button (Command1) on Form1, and set its Caption property to DropDown.
  6. Add the following code to the Command1 button's Click event:
       Sub Command1_Click ()
          x% = SendMessage(combo1.hWnd, CB_SHOWDROPDOWN, 1, 0&)
       End Sub
    
    						
  7. Place a second Command button (Command2) on Form1, and set its Caption property to Retract.
  8. Add the following code to the Command2 button's Click event:
       Sub Command2_Click ()
          x% = SendMessage(combo1.hWnd, CB_SHOWDROPDOWN, 0, 0&)
       End Sub
    
    						
  9. 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.

Notes

If the form is moved or resized while the List is down, the List will not move with the form. To make it behave like the standard Combo Box, add the following code to the Form's paint and resize events to force the retraction of the list:
   Sub Form_Paint ()
      x% = SendMessage(combo1.hWnd, CB_SHOWDROPDOWN, 0, 0&)
   End Sub
				

   Sub Form_Resize ()
      x% = SendMessage(combo1.hWnd, CB_SHOWDROPDOWN, 0, 0&)
   End Sub
				

You don't have to do this, if you want the List to be always on top, even when the form is moved or resized. The List can be retracted only by clicking the "Rectract" Button or choosing an item from the list.

Modification Type:MajorLast Reviewed:12/9/2003
Keywords:KB124057