devxlogo

Forcing List to Drop

Forcing List to Drop

Question:
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?

Answer:
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 = &H400Const CB_FINDSTRING = (WM_USER + 12)Const CB_FINDSTRINGEXACT = (WM_USER + 24)Const CB_SHOWDROPDOWN = (WM_USER + 15)Const CB_GETDROPPEDSTATE = (WM_USER + 23)
Put the following functions in your declaration section:
Declare Function SendMessageByString Lib “user” Alias “SendMessage” (ByValhWnd%, ByVal wMsg%, ByVal wParam%, ByVal lParam$) As LongDeclare Function SendMessageByNum Lib “user” Alias “SendMessage” (ByValhWnd%, ByVal wMsg%, ByVal wParam%, ByVal lParam&) As Long
Put the following code in your combo box’s keypress event:
Dim flgIsdropped As IntegerDim 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 SubEnd If’if the list box is not already dropped, then drop it ‘returns nonzero if list box is dropped, zero otherwiseflgIsdropped = 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,combobox.Text)
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. Thedisadvantage 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,combobox.Text)(LB_FINDSTRING is WM_USER+16)
If you want to find an exact match, the constant is:
LB_FINDSTRINGEXACT (WM_USER+35)
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)

See also  Professionalism Starts in Your Inbox: Keys to Presenting Your Best Self in Email
devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist