VB icon

AutoComplete textbox and comboboxes like Explorer does

Email
Submitted on: 2/15/2015 10:34:00 PM
By: Filipe Lage (from psc cd)  
Level: Intermediate
User Rating: By 16 Users
Compatibility: VB 5.0, VB 6.0
Views: 2008
 
     Auto-fills a textbox as you type in... Exactly the same way as the textbox/combobox when you execute the Run window. Autocompletes the paths as you type...

 

Windows API/Global Declarations:

Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
'**************************************
'Windows API/Global Declarations for :AutoComplete textbox and comboboxes like Explorer does
'**************************************
' 2006-08-28
' Revision 2
' Fixed combobox behaviour (thanks to Enmity)
' Enumerated AutoCompleteFlags should be in hex, not decimal as pointed out
' Added a sub to handle for you to call.
' Just use:
'SetAutoComplete Text1
' or
'SetAutoComplete ComboBox1
'
' And the function will handle the rest
Private Declare Function SHAutoComplete Lib "shlwapi.dll" (ByVal hWndEdit As Long, ByVal dwFlags As AutoCompleteFlags) As Integer
Private Declare Function FindWindowEx Lib "user32.dll" Alias "FindWindowExA" (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long
Public Enum AutoCompleteFlags
SHACF_DEFAULT = &H0
SHACF_FILESYSTEM = &H1
SHACF_URLHISTORY = &H2
SHACF_URLMRU = &H4
SHACF_USETAB = &H8
SHACF_URLALL = (SHACF_URLHISTORY Or SHACF_URLMRU)
SHACF_FILESYS_ONLY = &H10
SHACF_FILESYS_DIRS = &H20
SHACF_AUTOSUGGEST_FORCE_ON = &H10000000
SHACF_AUTOSUGGEST_FORCE_OFF = &H20000000
SHACF_AUTOAPPEND_FORCE_ON = &H40000000
SHACF_AUTOAPPEND_FORCE_OFF = &H80000000
End Enum
Private Sub SetAutoCompleteComboBox(ByVal lngHwnd As Long, Optional Opts As AutoCompleteFlags = SHACF_DEFAULT)
' Thanks go to enmity for this fix
Dim o_hwndEdit As Long
o_hwndEdit = FindWindowEx(lngHwnd, 0, "EDIT", vbNullString)
If o_hwndEdit <> 0 Then
SetAutoCompleteTextBox o_hwndEdit, Opts
End If
End Sub
Private Sub SetAutoCompleteTextBox(ByVal lngHwnd As Long, Optional Opts As AutoCompleteFlags = SHACF_DEFAULT)
SHAutoComplete lngHwnd, Opts
End Sub
Public Sub SetAutoComplete(obj As Object, Optional Opts As AutoCompleteFlags = SHACF_DEFAULT)
If TypeOf obj Is TextBox Then SetAutoCompleteTextBox obj.hWnd, Opts
If TypeOf obj Is ComboBox Then SetAutoCompleteComboBox obj.hWnd, Opts
End Sub
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
				
'**************************************
' Name: AutoComplete textbox and comboboxes like Explorer does
' Description:Auto-fills a textbox as you type in... Exactly the same way as the textbox/combobox when you execute the Run window. Autocompletes the paths as you type...
' By: Filipe Lage (from psc cd)
'**************************************

Create a form, add a textbox (text1)
Private sub Form_Load()
SetAutoComplete text1
end sub
' Finally, just start entering something like "C:\Windo" in your textbox and you'll notice that it autocompletes with "C:\Windows" (suggested) but you also have a list of possible paths begining with the text you type in.
' Just thought I could share this with you... Can be handy ;)


Other 6 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Intermediate category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.