The information in this article applies to:
- Standard and Professional Editions of Microsoft Visual Basic
for Windows, versions 2.0 and 3.0
SUMMARY
There is no built-in Visual Basic event that gets fired when the dropdown
list portion of a Combo box is retracted. This article shows by example how
to fire such an event by sending a message to the Combo box.
MORE INFORMATION
The Combo box can be polled to obtain the status of its dropdown list. By
constantly sending a CB_GETDROPPEDSTATE message to the Combo box inside a
timer event, you can detect the moment the list is retracted. The Windows
API SendMessage() is used for this purpose. SendMessage returns a non-zero
integer if the dropdown list is presently visible and 0 otherwise.
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 portion 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
Const WM_USER = &H400
Const CB_GETDROPPEDSTATE = (WM_USER + 23)
- Add the following code to the Load() event of Form1:
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) and Timer (Timer1) on Form1.
- Add the following code to the Combo box DropDown() event:
Sub Combo1_DropDown ()
Timer1.Interval = 1
Timer1.Enabled = True
End Sub
- Add the following code to the Timer() event:
Sub Timer1_Timer ()
status% = SendMessage(Combo1.hWnd, CB_GETDROPPEDSTATE, 0, 0&)
If Not status% Then
'Insert the code for the retraction event here
MsgBox "DropDown List Retracted!"
Timer1.Enabled = False
End If
End Sub
- Press the F5 key to run the program. When the dropdown button of the
combo is pressed and the list is later retracted by either choosing an
item from the list, clicking the dropdown button again, clicking
elsewhere on the screen, or pressing the ESC key, a message box pops up
announcing that the dropdown list has just retracted.
|