Browse DevX
Sign up for e-mail newsletters from DevX

Tip of the Day
Language: Visual Basic
Expertise: Beginner
Aug 6, 1997



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

Create An Access/Quicken-Like Combo Box

This example shows how to make a combo box act like those found in Quicken or Microsoft Access, without using a third-party VBX. As each character is typed, the elements in the combo box are searched and, if a match is found, retrieved. If a match is not found, the original typed text is restored. The only code needed resides in the KeyUp event of a combo box called combo1. The combo box is sorted alphabetically, so it stops on the first alphabetical match. I use the SendMessage API to turn the redraw of the combo box off and then on when the search is complete. Here are the form-level declarations for the combo box:
 Dim strCombo As String
Const KEY_A = 65
Const KEY_Z = 90
Declare Function SendMessage Lib "User" _
	(ByVal hWnd As Integer, _
	ByVal wMsg As Integer, _
	ByVal wParam As Integer, _
	lParam As Any) As Long
The code in the KeyUp event looks like this:
 Dim x%
Dim strTemp$
Dim nRet&

If Keycode >= KEY_A And Keycode <= KEY_Z Then
'only look at letters A-Z
	strTemp = combo1.Text
	If Len(strTemp) = 1 Then strCombo = strTemp
	nRet& = SendMessage(combo1.hWnd, _
		WM_SETREDRAW, False, 0&)
	For x = 0 To (combo1.ListCount - 1)
		If UCase((strTemp & _
		Mid$(combo1.List(x), _
		Len(strTemp) + 1))) =
UCase(combo1.List(x)) Then
		combo1.ListIndex = x
		combo1.Text = combo1.List(x)
		combo1.SelStart = Len(strTemp)
		combo1.SelLength = _
			Len(combo1.Text) - (Len(strTemp))
		strCombo = strCombo & _
			Mid$(strTemp, Len(strCombo) + 1)
		Exit For
		If InStr(UCase(strTemp), _
			UCase(strCombo)) Then
			strCombo = strCombo & _
			Mid$(strTemp, Len(strCombo) + 1)
			combo1.Text = strCombo
			combo1.SelStart = Len(combo1.Text)
			strCombo = strTemp
		End If
	End If
nRet& = SendMessage(combo1.hWnd, _
	WM_SETREDRAW, True, 0&)
End If
Dan Fox
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