How to Detect the Retraction of a Combo Box Dropdown List

Last reviewed: June 21, 1995
Article ID: Q124058
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

  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.


Additional reference words: 2.00 3.00 ComboBox
KBCategory: kbprg kbui
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.