How to Close VB Combo Box with ENTER key

Last reviewed: June 21, 1995
Article ID: Q84474
The information in this article applies to:

- Standard and Professional Editions of Microsoft Visual Basic for

  Windows, versions 2.0 and 3.0
- Microsoft Visual Basic programming system for Windows, version 1.0

SUMMARY

If you open a combo box and then use the ARROW keys to scroll through it, pressing the ENTER key will not close the combo box like a mouse click will. This is normal behavior. The following example demonstrates how to make a combo box close when the ENTER key is pressed.

MORE INFORMATION

The following program makes use of the Windows API SendMessage function to send the combo box the message to close. This is done only after the ENTER key is detected in the KeyPress event for the combo box.

Two Windows API Declare statements must be added to your application. These can be added either in the GLOBAL.BAS module, or in the general Declarations section of the form containing the combo box.

Steps to Reproduce Behavior

  1. Run Visual Basic for Windows, or from the File menu, choose New Project (press ALT, F, N) if Visual Basic for Windows is already running. Form1 is created by default.

  2. Add the following two declarations to the global module or the General Declarations for Form1:

       Declare Function SendMessage% Lib "user" (ByVal hWnd%, ByVal
                      wMsg%, ByVal wParam%, ByVal lParam&)
       Declare Function GetFocus Lib "user" () As Integer
    
       (Note that the first Declare statement must be on just one line, not
       split across two lines as it is here.)
    
    

  3. Place a combo box on Form1.

  4. Under the KeyPress event for the combo box, place the following code:

        If KeyAscii = 13 Then
    
            Const WM_USER = &h400
            Const  CB_SHOWDROPDOWN = WM_USER + 15
    
            Combo1.SetFocus
            BoxwHND% = GetFocus()
            r& = SendMessage(BoxwHND%, CB_SHOWDROPDOWN, 0, 0)
            KeyAscii = 0
        End If
    
    

  5. Place a command button on Form1.

  6. In the Click event for Command1, place the following code:

       ' This will add some data to the combo box.
       for i =1 to 10
          Combo1.AddItem STR$(i)
       Next i
    
    

  7. Press the F5 key to run the application.

  8. Choose the Command1 button to fill the combo box.

  9. Open the combo box with the mouse, and scroll down with the ARROW keys. Pressing the ENTER key will close the Combo Box.


Additional reference words: 1.00 2.00 3.00
KBCategory: kbprg
KBSubcategory: PrgCtrlsStd


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: June 21, 1995
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.