VBcoders Guest



Don't have an account yet? Register
 


Forgot Password?



AutoComplete textbox and comboboxes like Explorer does

by Filipe Lage (7 Submissions)
Category: Coding Standards
Difficulty: Intermediate
Date Added: Wed 3rd February 2021
Rating: (16 Votes)

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...

API Declarations
' 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

Rate AutoComplete textbox and comboboxes like Explorer does

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 ;)

Download this snippet    Add to My Saved Code

AutoComplete textbox and comboboxes like Explorer does Comments

No comments have been posted about AutoComplete textbox and comboboxes like Explorer does. Why not be the first to post a comment about AutoComplete textbox and comboboxes like Explorer does.

Post your comment

Subject:
Message:
0/1000 characters