How to Detect the Retraction of a Combo Box Dropdown List (124058)



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 Q124058

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

  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 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)
    
    						
  3. 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
    
    						
  4. Place a Combo box (Combo1) and Timer (Timer1) on Form1.
  5. Add the following code to the Combo box DropDown() event:
       Sub Combo1_DropDown ()
          Timer1.Interval = 1
          Timer1.Enabled = True
       End Sub
    
    						
  6. 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
    
    						
  7. 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.

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