ID Number: Q76513
1.00
WINDOWS
Summary:
This article explains how to instantly clear the contents of a Visual
Basic combo box by sending the combo box a CB_RESETCONTENT message.
This information applies to Microsoft Visual Basic programming system
version 1.0 for Windows.
More Information:
No single command within Visual Basic will clear out the entries of a
combo box. However, you can clear all entries at once with a simple
While loop, as follows:
Do While Combo1.ListCount > 0
Combo1.RemoveItem 0
Loop
If you want a single command to clear all combo box entries at once,
you can use the SendMessage Windows API function. The arguments to
SendMessage with the CB_RESETCONTENT parameter are
SendMessage(hWnd%, wMsg%, wParam%, lParam&)
where
hWnd% Identifies the window that is to receive the message
wMsg% The message to be sent (CB_RESETCONTENT = &H411)
wParam% Is not used (NULL)
lParam& Is not used (NULL)
Specifying wMsg% equal to &H411 sends a CB_RESETCONTENT message to
the combo box. This removes all strings from the combo box and frees
any memory allocated for those strings.
To get hWnd%, the handle to the target window, you must call the
Windows API function GetFocus. This method will return the handle to
the control that currently has focus. For a combo box with a Style
property of 2 (Dropdown List), this will return the handle to the
combo box that you want to send the message to. For other styles of
combo boxes, the focus is set to a child edit control that is part of
the combo box, and you must use the GetParent() Windows API function
to get the handle to the combo box itself.
The following steps demonstrate how to delete entries from a combo
box:
1. Create a combo box called Combo1 on Form1.
2. Declare the following Windows API functions at the module level or
in the Global section of your project:
Declare Function SendMessage% Lib "user" (ByVal hWnd%,_
ByVal wMsg%,_
ByVal wParam%,_
ByVal lParam&)
Declare Function GetFocus% Lib "user" ()
Declare Function PutFocus% Lib "user" Alias "SetFocus" (ByVal hWnd%)
Declare Function GetParent% Lib "user" (ByVal hWnd%)
(Note: Each Declare statement must be written on one line, leaving
out the underscore (_) line-continuation symbol shown above.)
3. Declare the following constants in the same section:
Global Const WM_USER = &H400
Global Const CB_RESETCONTENT = WM_USER + 11
Global Const DROP_DOWN_LIST = 2
4. Place some entries into the combo box in the Form_Load procedure:
Sub Form_Load ()
For i = 1 To 10
Combo1.AddItem Format$(i) 'Put something into combo box.
Next
End Sub
5. Create a Sub within the (Declarations) section of the Form1 Code
window with the following code:
Sub ClearComboBox (Combo As Control)
hWndOld% = GetFocus()
Combo.SetFocus
If Combo.Style = DROP_DOWN_LIST then
x = SendMessage(GetFocus(), CB_RESETCONTENT, 0, 0)
Else
x = SendMessage(GetParent(GetFocus()), CB_RESETCONTENT, 0, 0)
End If
Suc% = PutFocus(hWndOld%)
End Sub
6. Within an event procedure, call ClearComboBox with the name of the
Combo box as a parameter:
Sub Form_Click ()
ClearComboBox Combo1
End Sub
7. Run the program and click anywhere on Form1. This will clear the
combo box.
Reference(s):
"Programming Windows: the Microsoft Guide to Writing Applications for
Windows 3," Charles Petzold. Microsoft Press, 1990
"Microsoft Windows Software Development Kit Reference Volume 1,"
version 3.0
WINSDK.HLP file shipped with Microsoft Windows 3.0 Software
Development Kit
Additional reference words: 1.00 3.00