Browse DevX
Sign up for e-mail newsletters from DevX

Tip of the Day
Language: Visual Basic
Expertise: Beginner
Oct 1, 1996



Building the Right Environment to Support AI, Machine Learning and Deep Learning

Forcing List to Drop

I have loaded a ComboBox with several items. When running, if I click on the down arrow the items will display in the drop down list box OK and the user can select an item.. What code do I use to have the program show the drop down list automatically so the user does not have to click the down arrow to display the list?

I know of a couple of ways to do this. The first uses a combo box with the "style" property set to "0". Put the following constants in your declaration section:

Const WM_USER = &H400
Put the following functions in your declaration section:
Declare Function SendMessageByString Lib "user" Alias "SendMessage" (ByVal
hWnd%, ByVal wMsg%, ByVal wParam%, ByVal lParam$) As Long
Declare Function SendMessageByNum Lib "user" Alias "SendMessage" (ByVal
hWnd%, ByVal wMsg%, ByVal wParam%, ByVal lParam&) As Long
Put the following code in your combo box's keypress event:
Dim flgIsdropped As Integer
Dim lngRetval As Long

'if return was pressed, then set the combobox.listindex to the first match in the list
'The parameters (in order) are: window handle to the combo box, constant 'to find the first match in the list, the list position to start the 'search from, the text to search for.

If KeyAscii = 13 Then
   combobox.ListIndex = SendMessageByString(combobox.hWnd, CB_FINDSTRING, 0,combobox.Text)
   Exit Sub
End If

'if the list box is not already dropped, then drop it
'returns nonzero if list box is dropped, zero otherwise
flgIsdropped = SendMessageByNum(combobox.hWnd, CB_GETDROPPEDSTATE, 0, 0)

If Not flgIsdropped Then
    'set third paramater to true to show dropdown box, false to hide it
    lngRetval = SendMessageByNum(combobox.hWnd, CB_SHOWDROPDOWN, True, 0)
End If
Put the following code in your combo box's lostfocus event:
combobox.ListIndex = SendMessageByString(combobox.hWnd, CB_FINDSTRING, 0,
This code checks to see if the list box has already been dropped and drops it if need be. Once the list box gets dropped, Windows takes care of moving the list around to seek an entry that matches what the user has typed. The disadvantage to using this method is that even though Windows displays the entry that matches the typed string, I don't know how to get Windows to highlight the entry. If anybody knows how to do this, I would like to hear about it. The other option is to use a text box and a list box. Put the following code in the text box's change event:
listbox.ListIndex = SendMessageByString(combobox.hWnd, LB_FINDSTRING, 0,
If you want to find an exact match, the constant is:
Then put code in the text box's lost focus event that changes the text to the selected list index. This option will mimic the operation of the search box in help files. The disadvantage is that you have that big list box on your screen all the time. (Although I guess you could toggle the visible property)
DevX Pro
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date