How to Close VB Combo Box with ENTER keyID: Q84474
|
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.
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.
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.)
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
' This will add some data to the combo box.
for i =1 to 10
Combo1.AddItem STR$(i)
Next i
Additional query words: 2.00 3.00
Keywords :
Version :
Platform :
Issue type :
Last Reviewed: June 18, 1999