How to Programmatically Display or Hide a VB Combo Box ListID: Q85991
|
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 "%{Down}"
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:
' Enter each Declare statement as one, single line:
Declare Function GetFocus Lib "User" () as Integer
Declare Function GetParent Lib "User" (ByVal hWnd as Integer)
as Integer
Declare Function SendMessage Lib "User" (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
Sub Form_Load ()
Combo1.AddItem "apple"
Combo1.AddItem "orange"
Combo1.AddItem "banana"
End Sub
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
Additional query words: 2.00 3.00
Keywords :
Version :
Platform :
Issue type :
Last Reviewed: June 8, 1999